background image

 

 

好了,现在到调用 USVN_Config_Ini

 

类了

这个类自然走的就是 Zend_Loader_Autoloader:autoload("USVN_Config_Ini") 
这个函数第一步会去调用 getClassAutoloaders 获取这个类的
AutoLoader。getClassAutoloaders 里面增加了对 namespaceAutoloader 的选择和判断,由于我

 

们很少使用,直接跳过
 
这里返回的 loader

 

打印出来是这样的

代码如下:
 
Array ( [0] => Zend_Loader_Autoloader Object ( [_autoloaders:

protected

] => Array ( ) 

[_defaultAutoloader:

protected

] => Array ( [0] => Zend_Loader [1] => loadClass ) 

[_fallbackAutoloader:

protected

] => [_internalAutoloader:

protected

] => Array *RECURSION* 

[_namespaces:

protected

] => Array ( [Zend_] => 1 [ZendX_] => 1 [USVN_] => 1 [menus_] => 1 ) 

[_namespaceAutoloaders:

protected

] => Array ( ) [_suppressNotFoundWarnings:

protected

] => 

[_zfPath:

protected

] => ) [1] => _autoload ) 

 
 
其实就是前面设置的_internalAutoloader  

 
这里就会实际调用 Zend_Loader_Autoloader:_autoload ("USVN_Config_Ini") 
好了,现在就看到了 Zend_Loader_Autoloader:_autoload

 

函数

 

$callback

 = 

$this

->getDefaultAutoloader(); 

这里会获取默认的 Autoloader,什么是默认的 Autoloader? 看这个类初始定义,实际上是

array

('Zend_Loader', 'loadClass'); 

下面自然就调用的是 call_user_func(

$callback

$class

);即

Zend_Loader:loadClass("USVN_Config_Ini") 
 
首先 Zend_Loader 已经在 AutoLoader.php 中被

require

 

其次我们看看 Zend_Loader:loadClass 方法,这个方法第一步是检查异常,跳过。第二步是
将类分隔,拼凑成

$file

, 比如 USVN/Config/Ini.php,下面就直接调用 self::loadFile(

$file

null, true); 
 
接下来查看 self::loadFile  

首先_securityCheck 看类名中是否有非法字符,没有,就

include

了这个

$file

。这里的

$file

当然是相对路径,需要拼接上 include_path, 记得 include_path 是在哪里设置的吗?在程序
的一开始就设置了!好了,这里就把 USVN_Config_Ini

 

这个类读取进来了。

看到这里你就该明白了,如果你自己定义了一个类,并且注册了 Namespace,比如
USVN,那么你就应该在 include_path 下面创建一个同名文件夹(大小写必须区分),然
后你要引入的相对的文件路径名就是以类名的_

 

做分隔读入的。

 
到这里 AutoLoad 机制就阅读完了。