Zend Framework Tutorial autoloading usage _php instance

Source: Internet
Author: User
Tags autoload autoloader zend framework
This example describes the autoloading usage of the Zend Framework tutorial. Share to everyone for your reference, as follows:

I. Overview

Automatic loading is a mechanism that does not rely on manual PHP code writing. Refer to the»php manual for automatic loading, once the autoloader is defined and you try to use a class or interface that is not defined, it is automatically called.

With automatic loading, you don't have to worry about where the classes are stored in your project. Define a well-defined autoloader, 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 autoloader will automatically find the file.

In addition, automatic loading ensures that it is loaded only once and improves performance-so you can use it instead of require_once ().

The Zend Framework encourages the use of automatic loading and provides many tools for automating the loading of code libraries and application code. These tools are described below, and how to use them effectively.

Implementation conventions for automatic loading

Class Naming conventions

The Zend framework draws on the pear idea that the class name has a 1:1 relationship with the file system. Simply put, the underscore character ("_") replaces the directory separator to represent the path of 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 that the location of the class has been set through PHP include_path, which makes it possible to find the file name with the path set in relative include_path through include () and require ().

In addition, it is recommended to prefix the vendor name or project name. This means that all 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 local namespace.

Auto Loader Design Conventions

The Zend framework supports automatic loading via the Zend_loader_autoloader, and mainly provides the following objectives and design elements:

provides namespace matching. Returns false if the namespace prefix of the class is not a registered namespace.

Allows you to define the autoloader as an alternate autoloader. A team may be widely distributed, or use a namespace prefix that is defined in case it tries to match any namespace prefix. However, this practice is not recommended because it may lead to unnecessary lookups.
Allows the disable error prompt to be turned on. Therefore, by default, it should be turned off. During the development phase, you can enable it.

Automatic loading can be customized. Some developers do not want to use Zend_loader::loadclass () to automatically load, but still want to use the Zend Framework's automatic loading mechanism. Zend_loader_autoloader allows the use of custom automatic loading.

Allows the callback chain to be loaded automatically using SPL. The purpose of this is to allow additional autoloader to be specified.

Second, usage:

In general, you simply need to introduce the containing 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, any class with a namespace prefix of "Zend_" or "zendx_" can be loaded, as long as you have specified include_path.
What if you want to use a different namespace prefix? The best and easiest way 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 an "alternate" autoloader. This means that if the namespace is defined, it will attempt to load automatically.

$loader->setfallbackautoloader (TRUE);

(Note: This method is not recommended, try not to use it).

The internal implementation of Zend_loader_autoloader is using the Zend_loader::loadclass () load class. The method uses an 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. The following issues may be caused:

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: (but note that when Display_errors is enabled, the error log is always displayed.) )

$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 a 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 version, PHP now officially supports namespaces.

However, the namespace of the Zend framework is completely different from the namespace of PHP 5.3. The "namespace" referred to in the Zend framework refers to a class prefix. For example, the prefix "Zend_" for all Zend framework class names. This is the "namespace" that we specified.

The native PHP namespace is used in the Zend Framework 2.0.0.

In addition to being able to specify arbitrary callbacks, the Zend framework defines an interface zend_loader_autoloader_interface that requires the automatic loading of class implementations:

Interface zend_loader_autoloader_interface{public  function AutoLoad ($class);}

If you want to use the custom autoloader in the Zend framework, you can use the Zend_loader_autoloader pushautoloader () and Unshiftautoloader () methods.
By using these methods, the custom loader is appended to or before the internal auto loader of the Zend Framework.

Each method accepts an optional second parameter, the namespace prefix of the class. The autoloader only looks for the given class prefix. If the class prefix is not specified, the autoloader is skipped, which can 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,//-manage classes with the prefix ' my_ ': $loader->pushautoloader (' My_ Autoloader ', ' my_ ');//Prepend static method Foo_loader::autoload () to the stack,//-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

array TD align= "left" >
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, unregisters all the autoloader callbacks, and all registered namespaces.

autoload ($class) < Code class= "code" >string| FALSE
  • $class , required . A String class name to load.

Try to load a class.

setdefaultautoloader ($callback) zend_loader_autoloader
  • $callback , Required .

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

settings in the autoloader stack using the specific autoloader list. Each item in the Autoloader list must be phpcallback.

getautoloaders () < Span class= "type" >array n/a

getnamespaceautoloaders ($namespace)
  • $namespace , required

Get all the registered Autoloader to load a specific namespace.

registernamespace ($namespace) zend_loader_autoloader
  • $namespace , Required .

Register namespace. If $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
  • $namespace , Required .


getregisterednamespaces () array n/a


suppressnotfoundwarnings ($flag = null) boolean| Zend_loader_autoloader
  • $flag , optional .

error prompt

setfallbackautoloader ($flag) zend_loader_autoloader

    $flag , required .

Isfallbackautoloader () Boolean N/A

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.


More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

I hope this article is helpful to you in PHP programming.

  • 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.