2012-01-16 83 views
0

试图自动加载<root>/incl/classes文件夹中的类。自动加载类问题

问题是,当我给ex调用一些类的时候。 ip

 $ip= new ip(); 
     $ip=$ip->get(); 

PHP给出错误消息通知:未定义变量:路径。但实际上文件已经存在

enter image description here

我宣布所有不同的路径在页面的顶部。

define("ds", DIRECTORY_SEPARATOR); 
$path = array(); 
$path['root'] = $_SERVER['DOCUMENT_ROOT']; 
$path['common'] = $path['root'] . ds . "common"; 
$path['design'] = $path['root'] . ds . "design"; 
$path['contents'] = $path['root'] . ds . "contents"; 
$path['content_images'] = $path['root'] . ds . "content" . ds . "images"; 
$path['design_images'] = $path['root'] . ds . "design" . ds . "images"; 
$path['blocks'] = $path['contents'] . ds . "blocks"; 
$path['includes'] = $path['root'] . ds . "incl"; 
$path['pages'] = $path['contents'] . ds . "pages"; 
$path['classes'] = $path['includes'] . ds . "classes"; 

$files = glob("common" . ds . "*.php"); 
array_unshift($files, $path['common'] . ds . "settings.php", $path['common'] . ds . "db.php"); 
foreach ($files as $filename) 
    require_once $filename; 

//Auto loading classes 
function __autoload($class_name) { 
    if (file_exists($path['classes'] . ds . $class_name . '.php')) { 
     require_once($path['classes']. ds . $class_name . '.php'); 
    } else { 
     die($path['classes'] . ds . $class_name . '.php'); 
    } 
} 

用于测试目的添加die($path['classes'] . ds . $class_name . '.php');行。它输出\ip.php。我想知道,即使我之前声明了它,为什么它不会回显$ path ['classes']?

回答

3

这是一个范围界定问题。您的自动加载功能中不存在您的$path变量。这是一个全球变量,你需要明确地“邀请”它:

function __autoload($class_name) { 
    global $path; 

你实际上应该已经得到了一个关于此的通知。

+0

OMG)))我怎么没注意到?! ))无论如何thx。没有xdebug没有给出任何关于此的通知 – 2012-01-16 04:40:32