class Loader (View source)

\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_autoload, 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.


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()

Properties

protected string $_local_ns
protected string $_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()

protected string $_global_library
static protected Loader $_instance

Methods

bool
autoload(string $class_name)

No description

static Loader
getInstance(string $local_library_path = null, string $global_library_path = null)

No description

bool
registerLocalNamespace(string|string[] $name_prefix)

Register local class prefix name, \Yaf\Loader search classes in two library directories, the one is configured via application.library.directory(in application.ini) which is called local library directory; the other is configured via yaf.library (in php.ini) which is called global library directory, since it can be shared by many applications in the same server.


When an autoloading is triggered, \Yaf\Loader will determine which library directory should be searched in by examining the prefix name of the missed classname. If the prefix name is registered as a local namespace then look for it in local library directory, otherwise look for it in global library directory.


Note:

If yaf.library is not configured, then the global library directory is assumed to be the local library directory. in that case, all autoloading will look for local library directory. But if you want your Yaf application be strong, then always register your own classes as local classes.

string
getLocalNamespace()

No description

clearLocalNamespace()

No description

bool
isLocalName(string $class_name)

No description

static bool
import(string $file)

No description

setLibraryPath(string $directory, bool $global = false)

No description

string
getLibraryPath(bool $is_global = false)

No description

Details

bool autoload(string $class_name)

No description

Parameters

string $class_name

Return Value

bool

static Loader getInstance(string $local_library_path = null, string $global_library_path = null)

No description

Parameters

string $local_library_path
string $global_library_path

Return Value

Loader

bool registerLocalNamespace(string|string[] $name_prefix)

Register local class prefix name, \Yaf\Loader search classes in two library directories, the one is configured via application.library.directory(in application.ini) which is called local library directory; the other is configured via yaf.library (in php.ini) which is called global library directory, since it can be shared by many applications in the same server.


When an autoloading is triggered, \Yaf\Loader will determine which library directory should be searched in by examining the prefix name of the missed classname. If the prefix name is registered as a local namespace then look for it in local library directory, otherwise look for it in global library directory.


Note:

If yaf.library is not configured, then the global library directory is assumed to be the local library directory. in that case, all autoloading will look for local library directory. But if you want your Yaf application be strong, then always register your own classes as local classes.

Parameters

string|string[] $name_prefix

a string or a array of class name prefix. all class prefix with these prefix will be loaded in local library path.

Return Value

bool

string getLocalNamespace()

No description

Return Value

string

clearLocalNamespace()

No description

bool isLocalName(string $class_name)

No description

Parameters

string $class_name

Return Value

bool

static bool import(string $file)

No description

Parameters

string $file

Return Value

bool

Loader setLibraryPath(string $directory, bool $global = false)

Since: 2.1.4

No description

Parameters

string $directory
bool $global

Return Value

Loader

string getLibraryPath(bool $is_global = false)

Since: 2.1.4

No description

Parameters

bool $is_global

Return Value

string