autoloading use in the Zend Framework

Source: Internet
Author: User
Tags autoloader instance method zend zend framework


A. Goals and Design


1. Class Naming conventions

To understand the autoloading in the Zend Framework, first understand the relationship between the class name and the class file.

The Zend Framework is an idea borrowed from Perl, and class names and filesystem are one by one corresponding relationships. Simply put, ' _ ' is used as the delimiter for the directory in order to resolve the file path, and then add a '. php ' suffix. For example, "Foo_bar_baz" and "foo/bar/baz.php" files correspond.

2.Autoloader conventions and Design

The Zend Framework's automatic loading is supported by Zend_loader_autoloader.


two. Basic Autoloader Usage


In a simple scenario, it is possible to simply request this class and instantiate it. We use getinstance () to get this instance. For example:

Require_once ' zend/loader/autoloader.php ';
Zend_loader_autoloader::getinstance ();

By default, this will allow you to load any class with the namespace prefix "Zend_" or "zendx_" as long as they are under your include_path. If you want to use classes under the namespace of other classes, the best, simplest, method is to invoke the registernamespace () of the instance method. You can load them through a single namespace prefix, or an array.

For example:

Require_once ' zend/loader/autoloader.php ';
$loader = Zend_loader_autoloader::getinstance ();
$loader->registernamespace (' Foo_ ');
$loader->registernamespace (Array (' Foo_ ', ' bar_ '));

Alternatively, you can tell Zend_loader_autoloader to automatically load as "fallback". This means that it will attempt to resolve namespace prefixes for any class. However, it is not recommended to use this.

$loader->setfallbackautoloader (TRUE);


Three. Resource autoloading


For example:

path/to/some/resources/
|--forms/
|   '--guestbook.php        //Foo_form_guestbook
|--models/
|   | --dbtable/
|   |   '--guestbook.php    //Foo_model_dbtable_guestbook
|   | --guestbook.php        //Foo_model_guestbook
|   '--guestbookmapper.php  //Foo_model_guestbookmapper

1. Create a resource loader.

$loader = new Zend_loader_autoloader_resource (Array (
    ' basepath ' = '  path/to/some/resources/',
    ' namespace ' = ' Foo ',
));

2. Use the Zend_loader_autoloader_resourse::addresourcetype () function to define the resource type.

$loader->addresourcetype (' form ', ' Forms ', ' form ')
       ->addresourcetype (' model ', ' models ', ' model ')
       - >addresourcetype (' dbtable ', ' models/dbtable ', ' model_dbtable ');

3. After the definition we can use the following:

$form = new Foo_form_guestbook ();
$guestbook = new Foo_model_guestbook ();<strong>
</strong>



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.