The Yaf_Loader class
(Yaf >=1.0.0)
Introduction
Yaf_Loader introduces a comprehensive autoloading solution for Yaf.
The first time an instance of Yaf_Application is retrieved, Yaf_Loader will instance a singleton, and registers itself with spl_autoload. You retrieve an instance using the Yaf_Loader::getInstance()
Yaf_Loader attempt to load a class only one shot, if
failed, depend on yaf.use_spl_auload, if this
config is On Yaf_Loader::autoload() will return
FALSE
, thus give the chance to other autoload function. if it is Off
(by default), Yaf_Loader::autoload() will return
TRUE
, and more important is that a very useful warning will be triggered
(very useful to find out why a class could not be loaded).
Note:
Please keep yaf.use_spl_autoload Off unless there is some library have their own autoload mechanism and impossible to rewrite it.
By default, Yaf_Loader assume all library (class defined script) store in the global library directory, which is defined in the php.ini(yaf.library).
If you want Yaf_Loader search some classes(libraries) in the local class directory(which is defined in application.ini, and by default, it is application.directory . "/library"), you should register the class prefix using the Yaf_Loader::registerLocalNameSpace()
Let's see some examples(assuming APPLICATION_PATH is application.directory):
Example #1 Config example
// Assuming the following configure in php.ini: yaf.library = "/global_dir" //Assuming the following configure in application.ini application.library = APPLICATION_PATH "/library"
Example #2 Register localnamespace
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract{
public function _initLoader($dispatcher) {
Yaf_Loader::getInstance()->registerLocalNameSpace(array("Foo", "Bar"));
}
?>
Example #3 Load class example
class Foo_Bar_Test => // APPLICATION_PATH/library/Foo/Bar/Test.php class GLO_Name => // /global_dir/Glo/Name.php class BarNon_Test // /global_dir/Barnon/Test.php
Example #4 Load namespace class example
class \Foo\Bar\Dummy => // APPLICATION_PATH/library/Foo/Bar/Dummy.php class \FooBar\Bar\Dummy => // /global_dir/FooBar/Bar/Dummy.php
You may noticed that all the folder with the first letter capitalized, you can make them lowercase by set yaf.lowcase_path = On in php.ini
Yaf_Loader is also designed to load the MVC classes, and the rule is:
Example #5 MVC class loading example
Controller Classes => // APPLICATION_PATH/controllers/ Model Classes => // APPLICATION_PATH/models/ Plugin Classes => // APPLICATION_PATH/plugins/
Example #6 MVC class distinctions
Controller Classes => // ***Controller Model Classes => // ***Model Plugin Classes => // ***Plugin
Example #7 MVC loading example
class IndexController // APPLICATION_PATH/controllers/Index.php class DataModel => // APPLICATION_PATH/models/Data.php class DummyPlugin => // APPLICATION_PATH/plugins/Dummy.php class A_B_TestModel => // APPLICATION_PATH/models/A/B/Test.php
also, the directory will be affected by yaf.lowcase_path.Note:
As of 2.1.18, Yaf supports Controllers autoloading for user script side, (which means the autoloading triggered by user php script, eg: access a controller static property in Bootstrap or Plugins), but autoloader only try to locate controller class script under the default module folder, which is "APPLICATION_PATH/controllers/".
Class synopsis
Properties
- _local_ns
- _library
-
By default, this value is application.directory . "/library", you can change this either in the application.ini(application.library) or call to Yaf_Loader::setLibraryPath()
- _global_library
- _instance
Table of Contents
- 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 local class prefix
- Yaf_Loader::setLibraryPath — Change the library path
- Yaf_Loader::__sleep — The __sleep purpose
- Yaf_Loader::__wakeup — The __wakeup purpose
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-class.yaf-loader.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.