自动加载的原理是Zend_Application实例了Zend_Loader_Autoloader,使其调用了spl_autoload_register(array(__CLASS__, 'autoload')); 这样如果找不到的类就会调用此对象的autoload方法进行处理。
一个名称空间可以有多个加载器,迭代找出符合的自定义加载器后,其余的就不会使用了,一般我们只会针对一个名称空间注册一个自定义加载器或者直接使用默认的加载器。
注册名称空间方式分为两种:
- 只注册了空间的前辍,没有指定任何加载器,注册方法为registerNamespace('空间名称')。
- 注册了空间前辍,并指定了加载器。注册方法为pushAutoloader(加载器,'空间名称')或unshiftAutoloader(加载器,'空间名称')两个方法的区别是push将加载器放到指定空间名称的加载器队列之后,unshift是之前。
当一个类没有找到并要求载入时,其类名交给Zend_Loader_Autoloader::autoload()处理,会经历以下几个过程:
- 用已经注册名称空间前辍和此类名进行对比,找出名称空间指定的加载器。
- 用名称空间注册方式第二种名称前辍和此类名前辍进行对比,找出名称空间指定的加载器。
- 用名称空间注册方式第一种名称前辍和此类名前辍进行对比,如果存在则用Zend的加载器。
- 如果没有任何名称空间前辍和此类相等,而确设置了FallbackAutoloader标志则也使用Zend的加载器,否则不返回任何加载器,此类也就不能被载入了。
自定义的加载器有以下几种形式:
- 一个实现了Zend_Loader_Autoloader_Interface接口的对象
- 一个函数
- 以回调方式(callback)调用一个对象或类的方法.(回掉函数call_user_func)
$autoloader = Zend_Loader_Autoloader::getInstance(); $myAutoloaderClass = new my_Autoloader(); $autoloader->pushAutoloader($myAutoloaderClass, myNamespace);
$autoloader = Zend_Loader_Autoloader::getInstance(); function myAutoloaderFun(){ //TODO }; $autoloader->pushAutoloader('myAutoloaderFun', myNamespace);
$autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->pushAutoloader(array('类名', '方法'), myNamespace);
Zend加载器对象方法是Zend_Loader_Autoloader::_autoload。此方法会用call_user_func调用最终的加载方法如默认为array('Zend_Loader', 'loadClass')对当前对象进行加载。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)