zf2 启动简介:一,类的加载

AutoloaderFactory

自动加载工厂类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Zend\Loader\AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true,
'namespaces'=>array(
'Etah'=>__DIR__ . '/etah'
),
// 'prefixes' => array(
// 'Scapi' => __DIR__ . '/../library/Scapi',
// ),
'fallback_autoloader' => true
),
'Zend\Loader\ClassMapAutoloader' => array(
$zf2Path . '/autoload_classmap.php'
)
));

后面会介绍这段代码的意思

既然AutoloaderFactory是工厂类,那么它封装的自动加载类有那些呢

  • ClassMapAutoloader

    类地图自动加载器—类地图映射的方式加载类(zf2类库)

  • ModuleAutoloader

    模块自动加载器—同ClassMapAutoloader(模块类)

  • StandardAutoloader

    标准自动加载器—通过注册命名空间加载类 (默认加载方式)

Zend\Loader\AutoloaderFactory::factory()

factory方法参数数组的第一个配置元素必须为 Zend\Loader\StandardAutoloader

或者在 Zend\Loader\AutoloaderFactory::factory 调用之前手动载入 Zend\Loader\ClassMapAutoloader

  • StandardAutoloader

    • autoregister_zf == 是否自动注册Zend命名空间,若为false,下面需要手动注册

    • namespaces == 注册命名空间

    • prefixes 命名空间出现之前的和命名空间作用相似的东西,以_分割(一般用不上)

    • fallback_autoloader == 拼装实际类路径时可能会用到(一般情况下用不到,默认配成true)

  • ClassMapAutoloader

    • 类地图映射php文件

  • ModuleAutoloader

    • 从各种来源定位和按需加载的模块类(后续介绍)

这三个类都是通过 spl_autoload_register 函数注册自动加载函数

所以建议的Zend\Loader\AutoloaderFactory::factory()参数顺序

先配置 StandardAutoloader

然后再配置 ClassMapAutoloader

原因是:在载入类的时候会先调用 ClassMapAutoloader 中的加载类方法,若未找到则会调用 StandardAutoloader 中的类加载方法

ClassMapAutoloader 加载类的方式比 StandardAutoloader中更加的高效
前者只是在一个大的数组映射表中寻值,而后者则需要拼装截取才能获得最终的路径

以下为测试数据:(请求开始时间-请求结束时间)//i7 四核条件下(性能低的机器上会更加明显)

通过 StandardAutoloader 方式

start-time = 1394976543.9461

end-time = 1394976544.2273

耗时 0.2812 秒

通过 ClassMapAutoloader 方式

start-time = 1394976731.8825

end-time = 1394976732.2164

耗时0.3339 秒

速度提升16%

以下是以数组的方式打印自动装载器数组

1
2
3
4
5
6
$loader = \Zend\Loader\AutoloaderFactory::getRegisteredAutoloaders();
echo "<pre>";
print_r($loader);

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Array
(
[Zend\Loader\StandardAutoloader] => Zend\Loader\StandardAutoloader Object
(
[namespaces:protected] => Array
(
[Zend\] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/
[Etah\] => /Applications/XAMPP/xamppfiles/htdocs/zend/etah/
[Application\] => /Applications/XAMPP/xamppfiles/htdocs/zend/module/Application/src/Application/
)
[prefixes:protected] => Array
(
)
[fallbackAutoloaderFlag:protected] => 1
)
[Zend\Loader\ClassMapAutoloader] => Zend\Loader\ClassMapAutoloader Object
(
[mapsLoaded:protected] => Array
(
[0] => vendor/ZF2/library/autoload_classmap.php
)
[map:protected] => Array
(
[Zend\Authentication\Adapter\AbstractAdapter] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/AbstractAdapter.php
[Zend\Authentication\Adapter\AdapterInterface] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/AdapterInterface.php
[Zend\Authentication\Adapter\DbTable\AbstractAdapter] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/DbTable/AbstractAdapter.php
[Zend\Authentication\Adapter\DbTable\CallbackCheckAdapter] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/DbTable/CallbackCheckAdapter.php
[Zend\Authentication\Adapter\DbTable\CredentialTreatmentAdapter] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/DbTable/CredentialTreatmentAdapter.php
[Zend\Authentication\Adapter\DbTable\Exception\ExceptionInterface] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/DbTable/Exception/ExceptionInterface.php
[Zend\Authentication\Adapter\DbTable\Exception\InvalidArgumentException] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/DbTable/Exception/InvalidArgumentException.php
[Zend\Authentication\Adapter\DbTable\Exception\RuntimeException] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/DbTable/Exception/RuntimeException.php
[Zend\Authentication\Adapter\DbTable] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/DbTable.php
[Zend\Authentication\Adapter\Digest] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/Digest.php
[Zend\Authentication\Adapter\Exception\ExceptionInterface] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/Exception/ExceptionInterface.php
[Zend\Authentication\Adapter\Exception\InvalidArgumentException] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/Exception/InvalidArgumentException.php
[Zend\Authentication\Adapter\Exception\RuntimeException] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/Exception/RuntimeException.php
[Zend\Authentication\Adapter\Exception\UnexpectedValueException] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/Exception/UnexpectedValueException.php
[Zend\Authentication\Adapter\Http\ApacheResolver] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/Http/ApacheResolver.php
)
)
)

如上可以看出若要加载一个类文件,如 Zend\Authentication\Adapter\AbstractAdapter

若使用 StandardAutoloader 方式来加载

则内部原理为 在 Zend\Loader\StandardAutoloader 对象中获取 注册的namespaces数组

[Zend\] => /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/

中的 /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/

替换到 Zend\Authentication\Adapter\AbstractAdapter

最后为 /Applications/XAMPP/xamppfiles/htdocs/zend/vendor/ZF2/library/Zend/Authentication/Adapter/AbstractAdapter

在加上.php的后缀就为类的实际路径

ClassMapAutoloader 的加载方式是直接在 Zend\Loader\ClassMapAutoloader 对象的 map数组映射表中寻找,最后获得实际物理路径.

关于插件的加载日后补充!