The Yaf_Loader class provides a comprehensive solution for Yaf to automatically load data. During the first use, Yaf_Loader class of the instance for Yaf_Application retrieval provided a comprehensive solution for Yaf to automatically load the function. In the first use, the instance of Yaf_Application will be retrieved. Yaf_Loader implements the single-profit mode and registers itself with spl_autoload. Return its instance through Yaf_Loader: getInstance ()
Yaf_Loader only tries to load a class once. if it fails, the subsequent operations will depend on yaf. use_spl_auload: if this configuration item is On, Yaf_Loader: autoload (), FALSE will be returned, so as to give the opportunity to other automatic loading functions. If this configuration item is Off (default), Yaf_Loader: autoload () returns TRUE, the most important thing is to throw a very useful warning (useful for identifying a class loading failure ).
So if we still need to register our own automatic loading function to pay attention to the above situation, but we can also register our own automatic loading function to the first one, spl_autoload_register ('My _ autoloader ', true, true); if the third parameter is set to true, it indicates that spl_autoload_register () adds the function to the first of the queue, rather than the end of the queue.
By default, Yaf_Loader collects all libraries (class-defined scripts) and stores them in the global library directory defined by php. ini (yaf. library. If you want to use Yaf_Loader to search for a local class (library) (defined in application. ini, which is application by default. directory. "/libraray"), you need to use Yaf_Loader: registerLocalNameSpace () to register the local class prefix.
Yaf_Loader class abstract
Yaf_Loader {/* attribute */protected $ _ local_ns; protected $ _ library; protected $ _ global_library; static $ _ instance;/* method */public void autoload (void) public void clearLocalNamespace (void) private void _ clone (void) public _ construct (void) public static void getInstance (void) public Yaf_Loader getLibraryPath ([bool $ is_global = false]) public void getLocalNamespace (void) public static void import (void) public void isLocalName (void) public void registerLocalNamespace ([mixed $ prefix]) public Yaf_Loader setLibraryPath (string $ directory [, bool $ is_global = false]) private void _ sleep (void) private void _ wakeup (void )}
Member method list
Yaf_Loader: autoload-The autoload purpose
Yaf_Loader: clearLocalNamespace-The clearLocalNamespace purpose
Yaf_Loader ::__ clone-The _ clone purpose
Yaf_Loader ::__ construct-The _ construct purpose
Yaf_Loader: getInstance-The getInstance purpose
Yaf_Loader: getLibraryPath-get the library path
Yaf_Loader: getLocalNamespace-The getLocalNamespace purpose
Yaf_Loader: import-The import purpose
Yaf_Loader: isLocalName-The isLocalName purpose
Yaf_Loader: registerLocalNamespace-register a local class prefix
Yaf_Loader: setLibraryPath-change library path
Yaf_Loader ::__ sleep-The _ sleep purpose
Yaf_Loader ::__ wakeup-The _ wakeup purpose
The following describes the loading function rules of Yaf_Loader through an instance (assuming APPLICATION_PATH is application. directory)
Example #1 Config example
// Assuming the following configure in php.ini:yaf.libraray = "/global_dir"//Assuming the following configure in application.iniapplication.libraray = APPLICATION_PATH "/library"
Assume that the following local namespaces have been registered:
Example #2 register a local namespace
registerLocalNameSpace(array("Foo", "Bar")); }?>
Example of automatic loading:
Example #3 load class
Class Foo_Bar_Test => // APPLICATION_PATH/library/Foo/Bar/Test. php class GLO_Name => // global_dir/Glo/Name. phpclass BarNon_Test // global_dir/Barnon/Test. php in PHP 5.3, you can use the namespace: Example #4 to load the namespace class \ Foo \ Bar \ Dummy => // APPLICATION_PATH/library/Foo/Bar/Dummy. phpclass \ FooBar \ Bar \ Dummy => // global_dir/FooBar/Bar/Dummy. php
You may notice that the first letter of all folder names is in upper case. you can set yaf. lowcase_path = On in php. ini to lower case them.
Yaf_Loader is also designed to load MVC classes. the response rules are as follows:
Example #5 Example of MVC loading
Controller Classes =>// APPLICATION_PATH/controllers/Model Classes =>// APPLICATION_PATH/models/Plugin Classes =>// APPLICATION_PATH/plugins/
Yaf determines whether a class is a MVC Class by identifying the suffix of a class (this is the default one, you can also change the configuration item yaf. name_suffix to change it to the prefix recognition:
Example #6 MVC differences
Controller Classes => // ***ControllerModel Classes => // ***ModelPlugin Classes => // ***Pluginsome examples:Example #7 MVC loading exampleclass IndexController // APPLICATION_PATH/controllers/Index.phpclass DataModel => // APPLICATION_PATH/models/Data.phpclass DummyPlugin => // APPLICATION_PATH/plugins/Dummy.phpclass A_B_TestModel => // APPLICATION_PATH/models/A/B/Test.php