Autoloading in Zend Framework (autoload class in Zend Framework)

good Evening respected Hebraist. Present to your attention our translation of the article from the Learning Zend Framework.

Just want to mention that I am not an expert in the field of English language and do not claim 100% correct translation, although in recent years set out to study it as best you can, because I want to speak freely, to read, to write.

So, if you find any inaccuracies in my translation, if I have something translated or interpreted incorrectly, please contact me, I will correct your mistakes and you will be highly appreciated.

So the actual translation:


Introduction



Startup (autoloading) is a mechanism that eliminates the need to manually track dependencies between files and classes in your PHP code.

the relevant section of the PHP manual learn how to connect starting. Upon activation of this mechanism, the function of startup is automatically called when you use not previously defined class or interface.

Using the autoload option you don't have to worry about in what file the class is in your project. Having a good auto loaders you don't have to worry about where is the file which describes the class. You just refer to a class in code, and auto-loader takes care of
the search feature ads required class.

Moreover, the use of new startup can save a lot of time as you
no need to be concerned about whether or not already a file or not. You
no need to check whether the connected file and it does not happen so that the file will connect even
times, hence there is no need to use require_once(); for the file connection.

Zend Framework encourages the use of autoloading, and provides several tools to download
third-party classes and class loading inside your application. This guide explains the use of autoloading with the Zend Framework, and will help you to use these opportunities most effectively.

Objective and design


naming conventions for classes
.

In order to understand how to use autoloading in Zned Framework, to begin to understand
the relationship between class names and file names in this framework.

Zend Framework uses the name of the classes taken in PEAR, where between class names and file names there is a 1:1 relationship. Simply put in ZF sign "_" in the class name replaced with "/", and at the end of the class name is added the suffix ".php". For example the class name of the "Foo_Bar_Baz" points to the file "Foo/Bar/Baz.php". Additionally, assume the file search variable in PHP include_path. Which allows you to attach the files looking for them in the directories specified in this configurational options.

We recommend for all your projects to use a prefix. For example, all the names
classes in Zend Framework begin with the prefix "Zend_". This avoids name conflicts
when using different libraries. Within Zend Framework, we call such prefixes spaces
names ("namespaces"). do Not confuse with namespaces in PHP.

Such rules and agreements are commonly used in Zend Framework and we recommend you
to follow these rules.

Agreement and design of the autoloader


Realizuet Zend Framework startup class Zend_Loader_Autoloader
which is subject to the following conditions and has the following agreement:

1. Provides a mapping of namespaces. If the class prefix is absent in the registered
inside the app namespace, it returns FALSE. With this organization,
2. Allows you to enable fallback mode. When you activate this mode, the loader will attempt
look for classes regardless of "namespace" defined for the loader.

3. Allows you to disable error output if the loader cannot find the required class.
By default this option is disabled. However, if you wish, you can enable it.

4. Allows you to create callback functions, for example, when the user does not want
to use the native method Zend_Loader::loadClass(). But it wants to use
the mechanisms of the auto loader Zend_Loader_Autoloader.

5. Allows you to chain from a variety of loaders. Thus you can connect
its function to load classes, which for example do not fall under the agreement on naming
classes in Zend Framework. You can use your autoloader before and after standard
loader the Zend Framework.

Basic usage of the loader.



Now that we've covered the basics, you can go to practice and see how to use
class Zend_Loader_Autoloader.

The easiest way to connect a file for this class. And to the loader instance. It should also be noted
the loader the Zend Framework is built on the pattern singleton. So to get an object
used method of getInstance().

    require_once 'Zend/Loader/Autoloader.php'; the

  1. Zend_Loader_Autoloader::getInstance();


By default, the bootloader will automatically mount all classes beginning with the prefixes
"Zend_" or "ZendX_", the path to which is specified in the variable include_path.

What to do if you want to force the loader to look for your classes which are prefixed a great
from Zend_. You must use the method registerNamespace(). The method takes as a parameter a prefix or array of prefixes. After the prefixes were added by using this method,
the loader will automatically look for classes with the given prefix and upload them.

    require_once 'Zend/Loader/Autoloader.php';

    $loader = Zend_Loader_Autoloader::getInstance();

    $loader->registerNamespace('Foo_');

    $loader->registerNamespace(array('Foo_', 'Bar_'));



Additionally you can include a fallback mode. This means that the loader will look for all classes regardless of the prefix.

    $loader->setFallbackAutoloader(true);



warning
do Not use the fallback mode. We recommend you to refuse this option, the draft
Zend Framework, no matter how tempting it may seem.


Zend_Loader_Autoloader uses the Zend_Loader::loadClass() which in turn
isoplot function include() for the file connection. In the absence of the file include()
will return false. And PHP returns the warning "warning ERROR".

This can cause some problems.
1. If the option display_errors is active, a warning error will be sent to the output stream.
2. If mode is set to error_reporting showing warnings in your log will be written
all error alerts in file connection.

Of course you can disable the error output inside the Zend_Loader_Autoloader even if included
option display_errors. However, this option will allow you to drown out the only error in the output stream.
classes and register them in the loader.


note:
When I was wrote this manual, the release of PHP 5.3. Beginning with this version of PHP
supports namespaces. However, ZF was designed before this feature
appeared in PHP, and we have agreed within the framework to use a namespace
assigning class names prefixes. For example, the prefix Zend_ all the classes of the framework.
We plan to include support for php namespaces in the loader since the 2nd version
framework.


If you want to use its functions for autoload files and classes
you can add them to the boot loader Zend_Loader_Autoloader the methods pushAutoloader() and unshiftAutoloader(). These methods allow you to add your boot loader before or after the call to loader the Zend Framework, and form thus a chain of different loaders.

The advantages of this approach are that:
1. Each of these methods accepts an optional second parameter which is
prefix classes to load. Ie you can specify the boot loader the Zend Framework,
attached to the chain loader must process only classes with a certain prefix.
2. You will not have problems with download management, because all methods of a chain are functions
callback. which is devoid of the mechanism of spl_autoload_functions().

    // 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_');



startup resources.



When developing real applications there are situations when not all library applications
fall under the recommendations of the adopted names in the Zend Framework. This means that the auto-loader will not be able
to find the required files.

So, if you have read the topic about agreements and design of the boot loader that you can probably guess,
what Zend Framework can solve this problem. For this purpose there is the class Zend_Loader_Autoloader_Resource.
The resource implies the existence of a path name which will be the search for the necessary classes
and files. In the simplest case, it looks like this:

    $loader = new Zend_Application_Module_Autoloader(array(

    'namespace' => 'Blog',

    'basePath' => APPLICATION_PATH . '/modules/blog',

    ));



First, you need to inform the loader about the types of resources which are to be connected.
These types of are pair of the "key-value".

For example, take the following directory tree:

    the
  1. , path/to/some/resources/
  2. the
  3. |-- forms/
  4. | `-- Guestbook.php // Foo_Form_Guestbook the

  5. |-- models/
  6. | |-- DbTable/

    | | `-- Guestbook.php // Foo_Model_DbTable_Guestbook

    | `-- GuestbookMapper.php // Foo_Model_GuestbookMapper



To begin, create a resource:

    $loader = new Zend_Loader_Autoloader_Resource(array(

    'basePath' => 'path/to/some/resources/',

    'namespace' => 'Foo',

    ));



Then you define resource types. Method Zend_Loader_Autoloader_Resourse::addResourceType()
takes 3 arguments. The first argument is the name of the resource. The second argument, the directory in which to look for the resource relative to the root directory of the resource. The third parameter is the class prefix (or namespace).
In this example, you have 3 types of resource. the first form located in the forms directory and have the prefix "Form_". The second model located in the models directory and prefixed with Model_ and third dbTable in corresponding directories.

    $loader->addResourceType('form', 'forms', 'Form')

    ->addResourceType('model', 'models', 'Model')

    ->addResourceType('dbtable', 'models/DbTable', 'Model_DbTable');



After the announcement we can load our resources using the following classes:

    $form = new Foo_Form_Guestbook();

    $guestbook = new Foo_Model_Guestbook();



Opinion



Zend Framework encourages the use of autoloading (autoloading), and initializes the
this mechanism in the class Zend_Application.
I hope that this guide has given you the necessary information about how to use the Zend_Loader_Autoloader and helped you to understand its benefits, such as connecting their own autoloader classes (custom autoloaders) and auto loaders resources (resource autoloaders).

For detailed acquaintance with the components of the framework: Zend_Loader_Autoloader and Zend_Loader_Autoloader_Resource you can read the appropriate sections of the manual. Zend_Loader_Autoloader and Zend_Loader_Autoloader_Resource.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

The release of the new version of the module modLivestreet 0.3.0-rc

mSearch: search + filter for MODX Revolution

Emulator data from GNSS receiver NMEA