Automatic loading of classes under YAF framework

Source: Internet
Author: User

The first two blogs about PHP's own class loading and composer automatic loading, in fact, the YAF framework also implements the automatic loading of classes based on PSR0 and PSR4. Write this blog based on my understanding of how the Yaf class automatically loads. Because the contact YAF frame time is not long, therefore if has the wrong place, also hoped everybody feel free.

To understand the YAF framework of the automatic loading of classes, or the need for PSR0 and PSR4 specifications as the basis for the PSR0 and PSR4 specific specifications, this article does not do a detailed explanation. Yaf the automatic loading of classes can be divided into the automatic loading of global classes and local classes, and the automatic loading of classes related to the MVC framework, respectively.

  The automatic loading of global class and local class in YAF

First, explain the difference between the global class and the local class described in this article.

A global class is an existing class library at the so-called company or departmental level that can be used together in multiple projects, not just in the current project. The paths to these classes are typically specified by yaf.liabrary in the PHP configuration file php.ini.

A local class refers to a class that is applied only to this project, and the paths to which the classes are located can also be specified by Ap.liabrary in the YAF configuration file. In Yaf, the prefixes of those local classes can be declared by Yaf_loader::registerlocalnamespace (), and the class with that prefix needs to be loaded in the local class library.

The "_" in the class name is converted to the directory delimiter "/" when loaded using the PSR0 rule in yaf, whether it is a global class or a local class.

Depending on the blog you've seen, the load rules for global classes and local classes can be summarized as follows:

    1. If you specify Ap.library in the Application.ini of the specified yaf.library and product, either ap.library.namespace is specified in the Application.ini or by Yaf_loader:: Registerlocalnamespace () Registers a local namespace prefix, and when an undefined class is encountered in the program, it is automatically searched for the corresponding class in the ap.directory.library. (Ap.directory.library is an essential configuration for YAF, which specifies the absolute path address of the project)
    2. If a local ap.library is specified in the product's Application.ini file, but is not passed Ap.library.namespace or yaf_loader::registerlocalnamespace () Specify the local namespace, then either the yaf.library is specified, will go to the Yaf.library load class;
    3. The corresponding class is loaded in the local class library only if the local namespace is specified with the ap.library of the local class specified, and the class name contains a namespace prefix, otherwise the corresponding class is loaded into the file specified in Yaf.library. The namespace is specified in the following ways:
$loader = Yaf_loader::getignstance (); $loader->registerlocalnamespace (Array ("local"));

Or, specify Ap.library.namespace for local namespaces in Conf/application.ini in the project.

The following is the class name local_sublocal_test.php describes the above three cases corresponding to the loading rules.

    1. In this case, Ap.directory is typically specified in Application.ini as App_path (App_path is defined in the Portal file index.php), at which point the load path is app_path/library/local/ sublocal/test.php;
    2. In this case the loading path is generally yaf.library+ '/local/sublocal/test.php ';
    3. In this case the load path is ap.library+ '/local/sublocal/test.php ';

  Ii. automatic loading of classes related to the MVC framework

The automatic loading of classes related to the MVC framework is done by using a specific prefix or suffix to determine the load path of the class, completing the automatic loading of the class with a specific mapping of the file where the class is located: Class name = file name + suffix.

For controller controllers, the default module is {Project path}/controllers/, otherwise {Project path}/modules/{module name}/controllers. For example, Indexcontroller, the class automatically loads the file {project directory}/controllers/index.php.

For the model load path {Project directory}/models/, such as Usermodel automatically loads {project directory}/models/user.php.

View files related to V are not covered by this article.

Summary: This article is a summary of the automatic loading rules for classes in YAF, because the most currently used is the loading of classes that are related to MVC and the classes that specify the load path in Conf/application.ini, so only two cases are discussed. Other ways to load other classes in the project path will be discussed later with you.

Automatic loading of classes under YAF framework

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.