Yaf basic learning Summary: automatic loading of 5-Yaf classes

Source: Internet
Author: User
Tags autoloader
An important function of the framework is to automatically load the class. in the first demo, we agreed on the directory structure of our project, the framework automatically loads the required class files based on the directory structure. An important function of the framework is to automatically load the class. in the first demo, we agreed on the directory structure of our project, the framework automatically loads the required class files based on the directory structure.

During automatic startup, Yaf registers its own Autoloader through SPL. for performance considerations, for framework-related MVC classes, Yaf Autoloader only attempts to map directories once.

The specific directory ing rules are as follows:

For the Controller, the default module is {Project path}/controllers/; otherwise, it is {Project path}/modules/{module name}/controllers, such as IndexController, this automatically loads the file {project directory}/Controllers/Index. php

If the Model loading path is {project directory}/models/, for example, UserModel, {project directory}/models/User. php is automatically loaded.

Other rules, such as actions and plugin, are similar.

For non-framework MVC-related classes, Yaf supports two loading methods: global class and its own class. Yaf supports both case sensitive and non-sensitive methods to process file paths.

Global class and its own class (local class)

Yaf supports global and local loading methods to facilitate sharing of company-level shared libraries between different products deployed on one server.
Global class refers to the class shared among all products. The path of these class libraries is through ap. library in php. ini (of course, if PHP supports with-config-file-scan-dir during compilation, you can also write it in a separate ap. ini)

The local class refers to the product's own class library. the path of these class libraries is through the ap in the product configuration file. library configuration. in Yaf, you can call the registerLocalNamespace method of Yaf_Loader to declare that the class prefix is a local class.

Note: When use_spl_autoload is disabled, Yaf Autoloader will return immediately if it cannot be found at one time, and deprive the next automatic loader of the execution opportunity.

Loading rules of classes in Yaf

The class loading rules are the same: Yaf specifies that the class name must contain the path information, that is, the directory information separated by the underscore. yaf will automatically load according to the directory information in the class name. the following example shows how to declare a local class:

Example of a ing: Zend_Dummy_Foo
// Yaf will search for the class Foo_Dummy_Bar in the following path
{Library path (ap. library specified in php. ini)}/Foo/Dummy/Bar. php



If you call registerLocalNamespace in the following way: register a local class

// Declare that all classes starting with Foo and Local are Local classes
$ Loader = Yaf_Loader: getIgnstance ();
$ Loader-> registerLocalNamespace (array ("Foo", "Local "));

In the preceding example, Foo_Dummy_Bar will be searched in the following path.
// Yaf will search for the class Foo_Dummy_Bar in the following path
{Library path (ap. library specified in conf/application. ini)}/Foo/Dummy/Bar. php



In this example, you should know the Yaf class loading rules and distinguish between global classes and local classes.

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.