The example in this article describes the usage of Autoloading in the Zend Framework tutorial. Share it with everyone for your reference, the details are as follows:
1. Overview
Automatic loading is a mechanism that does not require manual writing of PHP code. Refer to » PHP Manual Autoloading. Once the autoloader is defined, it will be automatically called in case you try to use an undefined class or interface.
Using automatic loading, you don’t have to worry about the storage location of classes in the project. With a well-defined autoloader, you don't need to think about the location of a class file relative to the current class file, you just use the class and the autoloader will automatically find the file.
In addition, automatic loading ensures that it is loaded only once, which improves performance - so it can be used instead of require_once().
Zend Framework encourages the use of autoloading and provides many tools to automatically load code libraries and application code. Here's a look at these tools and how to use them effectively.
Autoloading implementation convention
Class naming convention
Zend Framework draws on the idea of PEAR, which is a 1:1 relationship between class names and file systems. Simply, replace the directory separator with the underscore character ("_") to represent the path to the file, and then add the suffix ".php". For example, the class "Foo_Bar_Baz" would correspond to "Foo/Bar/Baz.php" on the file system. Assuming that the location of the class has been set via PHP's include_path, this allows filenames to be found via include() and require() relative to the path set in include_path.
In addition, it is recommended to use the vendor name or project name as a prefix. This means that all classes you write have a common class prefix, for example, all code in Zend Framework is prefixed with "Zend_". This naming convention helps prevent naming conflicts. In ZendFramework, we often refer to the "namespace" prefix, be careful not to confuse it with PHP's local namespace.
Autoloader design convention
Zend Framework supports automatic loading through Zend_Loader_Autoloader, which mainly provides the following goals and design elements:
Provides namespace matching. If the namespace prefix of the class is an unregistered namespace, FALSE will be returned.
Allows defining an autoloader as an alternative autoloader. A team may be widely distributed, or use an undefined namespace prefix, in which case it will try to match any namespace prefix. However, this approach is not recommended because it may cause unnecessary lookups.
Allow enable and disable error prompts. Therefore, it should be off by default. During the development phase, it can be enabled.
Automatic loading can be customized. Some developers do not want to use Zend_Loader::loadClass() for automatic loading, but still want to use Zend Framework's automatic loading mechanism. Zend_Loader_Autoloader allows the use of custom autoloading.
Allows automatic loading of callback chains using SPL. The purpose of this is to allow additional autoloaders to be specified.
2. Usage:
Usually, you only need to introduce the containing class and then instantiate it. Due to the singleton mode adopted by Zend_Loader_Autoloader, you can use the getInstance() method to obtain an instance.
require_once 'Zend/Loader/Autoloader.php'; Zend_Loader_Autoloader::getInstance();
By default, any class with a namespace prefix of "Zend_" or "ZendX_" can be loaded, just make sure include_path is specified.
If you want to use other namespace prefixes? The best and easiest way is to call the registerNamespace() method. You can do this by passing a single namespace prefix, or an array:
require_once 'Zend/Loader/Autoloader.php'; $loader = Zend_Loader_Autoloader::getInstance(); $loader->registerNamespace('Foo_'); $loader->registerNamespace(array('Foo_', 'Bar_'));
Alternatively, you can use Zend_Loader_Autoloader as a "fallback" autoloader. This means that if the namespace is defined or not, autoloading will be attempted.
$loader->setFallbackAutoloader(true);
(Note: This method is not recommended, try not to use it).
The internal implementation of Zend_Loader_Autoloader uses Zend_Loader::loadClass() to load classes. This method uses include() to try to load the given class file. include() will return a boolean, or FALSE if not successful - and also issue a PHP warning. The following issues may result:
If display_errors is enabled, warnings will be included in the output.
Depending on the error_reporting level you configure, it can also be output to the log.
These error messages can be suppressed as follows: (But note that when display_errors is enabled, the error log will always be displayed.)
$autoloader->suppressNotFoundWarnings(true);
Select a version of Zend Framework
ZendFramework/
|-- 1.9.2/
| |-- library/
|-- ZendFramework-1.9.1-minimal/
| |-- library/
| -- 1.8.4PL1/
| |-- library/
|-- 1.8.4/
| |-- library/
|-- ZendFramework-1.8.3/
| |-- library/
|-- 1.7.8/
| |-- library/
|-- 1.7.7/
| |-- library/
|-- 1.7 .6/
| |-- library/
$autoloader->setZfPath($path, 'latest');
$autoloader->setZfPath($path, '1.8');
$autoloader->setZfPath($path, '1.7.7');
You can also use the configuration file
[production] autoloaderZfPath = "path/to/ZendFramework" autoloaderZfVersion = "1.7.7" [qa] autoloaderZfVersion = "1.8" [development] autoloaderZfVersion = "latest"
Autoloader interface
Note: Namespace prefix and PHP namespace
PHP5.3 has been released. In this version, PHP now officially supports namespaces.
然而,Zend Framework的命名空间和PHP 5.3的命名空间完全不同的。 Zend Framework中,提到的“命名空间”,是指一个类前缀。例如,所有的Zend Framework的类名称的前缀“Zend_”。 这是我们指定的“命名空间”。
在Zend Framework 2.0.0使用了原生的PHP命名空间。
自动加载器除了能够指定任意回调以外,Zend Framework还定义了一个需要自动加载类实现的接口Zend_Loader_Autoloader_Interface:
interface Zend_Loader_Autoloader_Interface { public function autoload($class); }
如果您希望在Zend Framework中使用自定义的自动加载器,可以使用 Zend_Loader_Autoloader的 pushAutoloader()和unshiftAutoloader()方法。
通过这些方法将在Zend Framework的内部自动装载器之后追加或之前使用自定义的加载器。
每个方法接受一个可选的第二个参数,类的命名空间前缀。自动加载器只查找给定的类前缀。如果不是指定的类前缀,将跳过自动加载器 , 这可能是一种性能改进方式。
当使用这个接口时,你需要传递类实例到Zend_Loader_Autoloader类的pushAutoloader()和unshiftAutoloader()方法,具体如下:
// Append function 'my_autoloader' to the stack, // to manage classes with the prefix 'My_': $loader->pushAutoloader('my_autoloader', 'My_'); // Prepend static method Foo_Loader::autoload() to the stack, // to manage classes with the prefix 'Foo_': $loader->unshiftAutoloader(array('Foo_Loader', 'autoload'), 'Foo_'); // Assume Foo_Autoloader implements Zend_Loader_Autoloader_Interface: $foo = new Foo_Autoloader(); $autoloader->pushAutoloader($foo, 'Foo_');
Zend_Loader_Autoloader的相关方法
希望本文所述对大家PHP程序设计有所帮助。
更多Zend Framework教程之Autoloading用法详解相关文章请关注PHP中文网!