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>