先说一下需求:
有这样一个目录结构:
|-- protected
[color="#FFFFFF"]----[/color]|-- models
[color="#FFFFFF"]--------[/color]|-- info
[color="#FFFFFF"]------------[/color]|-- item.php
[color="#FFFFFF"]------------[/color]|-- partner.php
[color="#FFFFFF"]--------[/color]|-- post.php
…
models 下有个info目录
info目录下有
item.php -> PHP Class: Info_Item
partner.php -> PHP Class: Info_Partner
现在就是要实现Yii支持 Info_Item 和 Info_Partner的自动加载.
和往常一样配置:
‘import’=>array(
'application.models.*',
'application.components.*',
),
运行后才发现不可以。
原来Yii::import(‘application.models.*’);
就是把 application.models 的真实地址加入到 php的includePath里面
if(self::$_includePaths===null)
{
self::$_includePaths=array_unique(explode(PATH_SEPARATOR,get_include_path()));
if(($pos=array_search('.',self::$_includePaths,true))!==false)
unset(self::$_includePaths[$pos]);
}
array_unshift(self::$_includePaths,$path);
set_include_path('.'.PATH_SEPARATOR.implode(PATH_SEPARATOR,self::$_includePaths));
return self::$_imports[$alias]=$path;
然后自动加载里面直接 include($className.’.php’):
public static function autoload($className)
{
// use include so that the error PHP file may appear
if(isset(self::$_coreClasses[$className]))
include(YII_PATH.self::$_coreClasses[$className]);
else if(isset(self::$_classes[$className]))
include(self::$_classes[$className]);
else
{
include($className.'.php');
return class_exists($className,false) || interface_exists($className,false);
}
return true;
}
不知道大家有没有明白我的意思,就是多层目录结构里类的自动载入。
想问问大家是如何解决这种问题的?