Autoloading usage in Zend Framework tutorial, zendautoloading

Source: Internet
Author: User
Tags autoload autoloader zend framework

Autoloading usage in Zend Framework tutorial, zendautoloading

This example describes how to use Autoloading In the Zend Framework tutorial. We will share this with you for your reference. The details are as follows:

I. Overview

Automatic loading is a mechanism that does not require manual coding of PHP code. Refer to» PHP manual automatic loading. Once the automatic loader is defined, it will be automatically called when you try to use an undefined class or interface.

With automatic loading, you do not have to worry about the storage location of classes in the project. Define a well-defined automatic 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, and the automatic loader will automatically find the file.

In addition, automatic loading ensures that the load is only once, improving the performance-so you can use it to replace require_once ().

Zend Framework encourages automatic loading and provides many tools for automatic loading of code libraries and application code. The following describes these tools and how to use them effectively.

Automatic Loading Implementation Convention

Class naming conventions

Zend Framework draws on the idea of PEAR, that is, the relationship between class names and file systems. Simply put, replace Directories With the underscore ("_") to represent the path of the file, and then add the suffix ". php ". For example, the "Foo_Bar_Baz" class corresponds to "Foo/Bar/Baz. php" on the file system ". Assuming that the class location has been set through the include_path in PHP, this allows you to find the file name in the path set in relative include_path through include () and require.

We recommend that you use the vendor name or project name as the prefix. This means that all the classes you write have a common class prefix. For example, all the code prefixes of Zend Framework are "Zend _". This naming convention helps prevent name conflicts. In ZendFramework, we often mention the "namespace" prefix. Be sure not to confuse it with the PHP local namespace.

Automatic loader design convention

Zend Framework supports automatic loading through Zend_Loader_Autoloader. It mainly provides the following objectives and design elements:

Provides namespace matching. If the namespace prefix of a class is an unregistered namespace, FALSE is returned.

You can define an automatic loader as a backup automatic loader. A team may be widely distributed, or use a defined namespace prefix, it will try to match any namespace prefix. However, this method is not recommended because it may lead to unnecessary searches.
Enable forbidden error prompt. Therefore, it should be disabled by default. You can enable it in the development phase.

You can customize automatic loading. Some developers do not want to use Zend_Loader: loadClass () for automatic loading, but still want to use the Zend Framework's automatic loading mechanism. Zend_Loader_Autoloader allows custom automatic loading.

You can use the SPL to automatically load callback chains. The purpose is to allow the specified auto-loader.

Ii. Usage:

Generally, you only need to introduce the include class and then instantiate it. Because Zend_Loader_Autoloader adopts the singleton mode, 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 the namespace prefix "Zend _" or "ZendX _" can be loaded, as long as include_path is specified.
If you want to use another namespace prefix? The best and simplest method is to call 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 a "slave" automatic loader. This means that the namespace will be automatically loaded regardless of whether it is defined.

$loader->setFallbackAutoloader(true);

(Note: This method is not recommended and should not be used as much as possible ).

The internal implementation of Zend_Loader_Autoloader uses Zend_Loader: loadClass () to load classes. This method uses include () to try to load a given class file. Include () returns a Boolean value. If not, FALSE is returned-and a PHP warning is issued. The following problems may occur:

If display_errors is enabled, the warning is included in the output.

It can also be output to logs based on the configured error_reporting level.
You can disable these error messages as follows: (however, when display_errors is enabled, the error log is always displayed .)

$autoloader->suppressNotFoundWarnings(true);

Select a Zend Framework Version

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.

However, the namespace of Zend Framework is completely different from that of PHP 5.3. The namespace mentioned in Zend Framework refers to a class prefix. For example, the prefix of all Zend Framework class names is "Zend _". This is the specified namespace ".

The native PHP namespace is used in Zend Framework 2.0.0.

In addition to the ability to specify any callback, the Zend Framework also defines an interface Zend_Loader_Autoloader_Interface for automatic loading class implementation:

interface Zend_Loader_Autoloader_Interface{  public function autoload($class);}

If you want to use a custom automatic loader in Zend Framework, you can use the pushAutoloader () and unshiftAutoloader () Methods of Zend_Loader_Autoloader.
These methods are used to append or use a custom loader after the internal automatic loader of Zend Framework.

Each method accepts an optional second parameter, the namespace prefix of the class. The automatic loader only looks for the given class prefix. If it is not the specified class prefix, the auto-loader will be skipped, which may be a way to improve performance.

When using this interface, you need to pass the pushAutoloader () and unshiftAutoloader () Methods of the class instance to the Zend_Loader_Autoloader class, as follows:

// 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

Method Return Value Parameters Description
GetInstance () Zend_Loader_Autoloader N/

Get instance

ResetInstance () void N/

Reset the status of the Zend_Loader_Autoloadersingleton instance, restore its original status, cancel all auto-loaders, and reconcile all registered namespaces.

Autoload ($ class) string|FALSE
  • $ Class, Required. A string class name to load.

Try to load a class.

Setdefaautoautoloader ($ callback) Zend_Loader_Autoloader
  • $ Callback, Required.

Specify the default loader callback

Getdefaautoautoloader () callback N/

Obtains the default loader interface. The default value is Zend_Loader: loadClass ().

SetAutoloaders (array $ autoloaders) Zend_Loader_Autoloader
  • $ Autoloaders, Required.

Set the auto-loader list to be used on the auto-loader stack. Each item in the auto-loader list must be PHPcallback.

GetAutoloaders () Array N/

 

GetNamespaceAutoloaders ($ namespace) Array
  • $ Namespace, Required

Obtain all registered auto loaders to load a specific namespace.

RegisterNamespace ($ namespace) Zend_Loader_Autoloader
  • $ Namespace, Required.

Register namespace. If$ NamespaceIs a string, it registers that namespace; if it's an array of strings, registers each as a namespace.

UnregisterNamespace ($ namespace) Zend_Loader_Autoloader
  • $ Namespace, Required.


GetRegisteredNamespaces () Array N/


SuppressNotFoundWarnings ($ flag = null) boolean|Zend_Loader_Autoloader
  • $ Flag, Optional.

Error Message

SetFallbackAutoloader ($ flag) Zend_Loader_Autoloader
  • $ Flag, Required.

 

IsFallbackAutoloader () Boolean N/

 

GetClassAutoloaders ($ class) Array
  • $ Class, Required.

 

UnshiftAutoloader ($ callback, $ namespace = '') Zend_Loader_Autoloader
  • $ Callback, Required. A valid PHPCallback

  • $ Namespace, Optional. A string representing a class prefix namespace.

 

PushAutoloader ($ callback, $ namespace = '') Zend_Loader_Autoloader
  • $ Callback, Required. A valid PHPCallback

  • $ Namespace, 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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.