1. dynamically load files and Classes
The zend_loader class can help you dynamically load files or classes.
Zend_loader is the most suitable file name for loading variables. For example, the file name to be loaded comes from or depends on user input. If the file name you load is a constant, zend_loader has no advantage.
1.1 load a file
zend_loader: LoadFile ($ filename, $ dirs = NULL, $ once = false);
static method zend_loader: LoadFile () Load PHP file, the loaded file can contain any php Code . In fact, the include function of PHP is encapsulated into a method. When the file inclusion fails, A zend_exception is thrown. php
zend_loader :: LoadFile ( $ filename , $ dirs = null , $ once = false );
$ Filename specifies the file to be loaded.
$ Dirs specifies the directory where the file is located
$ Once: If the boolean type is true, use include_once. Otherwise, use include.
1.2 load class
Zend_loader: loadclass ($ class, $ dirs = NULL)
This method is used to load a PHP class file. The file name format is "$ classname. php", that is, the name of the loaded file must be the same as the class name.
Loadclass checks whether the class in the file exists. <? PHP
Zend_loader :: Loadclass ( ' Container_tree ' ,
Array (
' /Home/production/mylib ' ,
' /Home/production/MyApp '
)
);
Class names use underlines as Directory separation lines, corresponding to the PHP file under the corresponding directory, and add ". php". For example, container_tree will point to container/tree. php.
If the file does not exist or the class in the file does not exist, loadclass () will throw a zend_exception.
1.3 determine whether a file exists and is readable
Zend_loader: isreadable ($ pathname)
Returns true or false to determine whether a file exists and is readable.
1.4 Use autoloader
Zend_loader: registerautoload ($ class = 'zend _ loader ', $ enable = true );
The system automatically tries to load class files without calling the LoadFile or loadclass method every time.
This method does not need to be called to include class files every time a class is used. In this way, when a class that is not included is included, the zend_loader class is automatically used to try to include $ class. ". PHP "file.
You can inherit a class from zend_loader and rewrite the zend_loader: autoload () method to automatically load your own rules.
Myloader. php <? PHP
Require_once ' Zend/loader. php ' ;
Class Myloader Extends Zend_loader
{
Public Static Function Loadclass ( $ Class , $ Dirs = Null )
{
Parent :: Loadclass ( $ Class , $ Dirs );
}
Public Static Function Autoload ( $ Class )
{
Try {
Self :: Loadclass ( $ Class );
Return $ Class ;
} Catch ( Exception $ E )
{
Return False ;
}
}
}
Zend_loader :: Registerautoload ( " Myloader " );
Then, you need to include myloader. php In the PHP code that automatically loads the class file.