This article describes the method of Zend Framework using the Zend_Loader component to dynamically load files and classes. Share it for your reference, as follows:
Loading the file
The Zend_Loader component can implement the loading function of files and can also determine whether the file is readable.
These two functions are implemented by the Zend_loader::loadFile() method and the Zend_loader::isReadable() method respectively.
Dynamic loading is a process in which files referred to by variables can be loaded. When a file that needs to be loaded is input to the user or parameters of a certain method, it will be difficult to load the file through traditional loading methods.
This process can be achieved through dynamic loading.
Example:
First, create the loaded document, which only implements a simple output function, the code is as follows.
<?php echo "Hello World!"; ?>
Main program:
<?php require_once("Zend/"); $filename = ''; Zend_Loader::loadFile($filename);
Execution results:
Hello World!
The description file has been successfully loaded.
Determine file attributes
Syntax format:
Zend_Loader::isReadable($filename)
If the file is readable, the program will return True
This method is encapsulated by the PHP function is_readable(), but there are still differences between the two. is_readable() method
The files in the include_path directory will not be automatically searched, while the Zend::isReadable() method is OK.
Example:
<?php require_once("Zend/"); $filename = ''; if(Zend_Loader::isReadable($filename)){ echo "document".$filename."It's readable!"; }else{ echo "document".$filename."Unreadable!"; } echo "<p/>"; if(is_readable('Zend/')){ echo "The file under include_path is readable"; }else{ echo "The file under include_path is not readable"; } echo "<p/>"; if(Zend_Loader::isReadable('Zend/')){ echo "The file under include_path is readable"; }else{ echo "The file under include_path is not readable"; }
result:
The file is readable! include_pathThe file below is unreadable include_pathThe following file can be read
This example shows that the is_readable() method cannot determine whether the include_path file is readable, while the Zend_Loader::isReadable() method can determine. include_path file, refers to the file introduced in the configuration.
Not only that, Zend_Loader can also load classes. This will be explained below.
Dynamic loading of classes
Zend_Loader can not only load files dynamically, but also implement them through its loadClass() method.
grammar:
Zend_Loader::loadClass($class,$dirs)
Where, class is the specified class name, class is the specified class name, and dirs is the path and file name where the file containing the class is located. The class name will correspond to the corresponding directory according to the underscore
PHP files, such as Zend_Controller_Action, will point to Zend/Controller/.
If the parameter $dirs is a string or array, the method will search for the corresponding directory in order and install the first matching file. If the file does not exist, the method will also search in the directory specified by include_path.
Example:
<?php require_once("Zend/"); Zend_Loader::loadClass('Zend_Date'); $date = new Zend_Date(); echo $date;
result:
2013-3-18 afternoon05:30:16
Explanation: This code directly loads the required class in include_path. Then instantiate and output.
For more information about Zend, please visit the special topic of this site:Zend FrameWork Framework Introduction Tutorial》、《Summary of excellent development framework for php》、《Yii framework introduction and common techniques summary》、《ThinkPHP Introduction Tutorial》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope that this article will be helpful to everyone's PHP programming based on the Zend Framework framework.