This example describes the autoloading usage of the Zend Framework tutorial. Share to everyone for your reference, specific as follows:
I. Overview
Automatic loading is a mechanism that requires no reliance on manual writing of PHP code. The reference»php manual is automatically loaded, and once the auto loader is defined, you attempt to use an undefined class or interface, and it is automatically invoked.
With automatic loading, you don't have to worry about where the class is stored in the project. Define a well-defined auto loader, you do not need to consider the location of a class file relative to the current class file, you only need to use the class, the automatic loader will automatically find the file.
In addition, automatic loading ensures that only one load is loaded and performance is enhanced-so you can use it instead of require_once ().
The Zend Framework encourages the use of automatic loading and provides many tools to automate loading of code libraries and application code. These tools are described below, and how they can be used effectively.
Implementation conventions that are loaded automatically
Class Naming conventions
The Zend framework draws on the idea of pear, which is the relationship between class name and file system 1:1. Simply put, the underscore character ("_") replaces the directory separator to represent the path to the file, and then adds the suffix ". php". For example, the class "Foo_bar_baz" will correspond to "foo/bar/baz.php" on the file system. Suppose you have set the location of the class through PHP's Include_path, which allows you to find the file name by using include () and require () to locate the path set in the relative include_path.
In addition, it is recommended that you use a vendor name or project name as a prefix. This means that all of the classes you write have a common class prefix, for example, all code prefixes for the Zend framework are "Zend_". This naming convention helps prevent naming conflicts. In Zendframework, we often refer to the "namespace" prefix, and be careful not to confuse it with PHP's native namespace.
Auto Loader Design Conventions
The Zend framework supports automatic loading through the Zend_loader_autoloader implementation, primarily providing the following objectives and design elements:
provides namespace matching. Returns false if the namespace prefix for the class is not a registered namespace.
Allows the automatic loader to be defined as an alternate automatic loader. A team may be widely distributed, or it will attempt to match any namespace prefix with a defined namespace prefix. However, this practice is not recommended because it may result in unnecessary lookups.
Allow the disable error prompt to be turned on. Therefore, by default, it should be in the off state. Development phase, you can enable it.
Automatic loading can be customized. Some developers do not want to use Zend_loader::loadclass () to load automatically, but still want to use the Zend Framework's automatic loading mechanism. Zend_loader_autoloader allows you to use custom automatic loading.
Allows automatic load of the callback chain with SPL. The purpose of this is to allow for the designation of additional loaders.
Second, use:
Typically, you just need to introduce the inclusion class and then instantiate it. Because of the singleton pattern used by Zend_loader_autoloader, you can use the getinstance () method to get an instance.
Require_once ' zend/loader/autoloader.php ';
Zend_loader_autoloader::getinstance ();
By default, you can load any class that has a namespace prefix of "Zend_" or "zendx_", as long as you ensure that include_path is specified.
What if you want to use a different namespace prefix? The best and easiest way to do this is to invoke the Registernamespace () method. You can pass 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 an "alternate" loader. This means that if the namespace is defined, it will try to load automatically.
$loader->setfallbackautoloader (TRUE);
(Note: This method is not recommended, try not to use).
The internal implementation of the Zend_loader_autoloader is to load the class using the Zend_loader::loadclass (). The method uses include () to attempt to load the given class file. Include () returns a Boolean value that returns False if not successfully-and also emits a PHP warning. May cause the following problems:
If Display_errors is enabled, the warning is included in the output.
Depending on the level of error_reporting you configure, it can also be exported to the log.
You can suppress these error messages, as follows: (Note that the error log is always displayed when Display_errors is enabled.) )
$autoloader->suppressnotfoundwarnings (TRUE);
Select a version of the 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 prefixes and PHP namespaces
PHP5.3 has been released. In this release, PHP now has formal support for namespaces.
However, the namespace of the Zend framework is completely different from the PHP 5.3 namespace. In the Zend framework, the reference to "namespaces" refers to a class prefix. For example, all of the Zend framework's class names are prefixed with "zend_". This is the "namespace" we specified.
The native PHP namespace was used in the Zend Framework 2.0.0.
In addition to being able to specify arbitrary callbacks, the Zend framework also defines an interface that requires the automatic loading of class implementations Zend_loader_autoloader_interface:
Interface Zend_loader_autoloader_interface
{public
function autoload ($class);
}
If you want to use a custom auto loader in the Zend framework, you can use the Pushautoloader () and Unshiftautoloader () methods of Zend_loader_autoloader.
These methods will use the custom loader after the internal automatic loader of the Zend Framework.
Each method accepts an optional second argument, the namespace prefix of the class. The auto loader finds only the given class prefix. If it is not the specified class prefix, the auto loader will be skipped, which may be a performance improvement.
When using this interface, you need to pass the class instance to the Pushautoloader () and Unshiftautoloader () methods of the Zend_loader_autoloader class, as follows:
Append function ' My_autoloader ' to the stack,
//To manage classes with the prefix ' my_ ':
$loader->pushautol Oader (' 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_ ');
Related Methods of Zend_loader_autoloader
| method return
Value |
Parameters |
Description |
GetInstance () |
Zend_loader_autoloader |
N/A |
Get instance |
Resetinstance () |
void |
N/A |
Resets the state of the Zend_loader_autoloadersingleton instance, restores its original state, and logs out all the Autochanger callbacks and all registered namespaces.
|
AutoLoad ($class) |
string|FALSE |
|
An attempt was made to load a class. |
Setdefaultautoloader ($callback) |
Zend_loader_autoloader |
|
Specifies the default loader callback |
Getdefaultautoloader () |
callback |
N/A |
Gets the default loader interface; The default is Zend_loader::loadclass (). |
Setautoloaders (Array $autoloaders) |
Zend_loader_autoloader |
|
Set up in the auto loader stack using a specific list of automatic loaders. Each item in the Auto loader list must be phpcallback.
|
Getautoloaders () |
Array |
N/A |
|
Getnamespaceautoloaders ($namespace) |
Array |
|
Gets all registered auto loaders to load a specific namespace.
|
registernamespace ($namespace) |
zend_loader_autoloader |
$namespace , required . The |
$namespace is A string, it registers that namespace; If it's an array of strings, registers each as a namespace. |
Unregisternamespace ($namespace) |
Zend_loader_autoloader |
|
|
Getregisterednamespaces () |
Array |
N/A |
|
Suppressnotfoundwarnings ($flag = null) |
boolean|Zend_Loader_Autoloader |
|
Error tips |
Setfallbackautoloader ($flag) |
Zend_loader_autoloader |
|
|
Isfallbackautoloader () |
Boolean |
N/A |
|
Getclassautoloaders ($class) |
Array |
|
|
unshiftautoloader ($callback, $namespace = ") |
zend_loader_autoloader |
-
$callback , required . A valid php callback
-
, optional . A string representing a class prefix namespace.
|
|
pushautoloader ($callback, $namespace = ') |
zend_loader_autoloader |
$callback , . A valid php callback , optional . A string representing a class prefix namespace. |
|
Removeautoloader ($callback, $namespace = ") |
Zend_loader_autoloader |
$callback,required. A valid PHPcallback
$namespace,optional. A string representing a class prefix namespace, or an array of namespace strings.
|
More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design.