Home > Backend Development > PHP Tutorial > A quick note on instantiating classes in PHP

A quick note on instantiating classes in PHP

WBOY
Release: 2016-07-29 08:37:30
Original
1093 people have browsed it

The following is a function that calls a model (Module). The basic function of this function is to specify the name of a model (abstracted into a class), and then it will search for the script of this class in the model directory and return it after instantiation. One advantage of this approach is that loading and instantiation are automatic, giving you maximum flexibility. Now please look at the following code, it is not long and not complicated:
function &load_class($class_name, $param = null, $instantiate = true)
{
static $objects = array();
$class_name = ucfirst(strtolower ($class_name));
if (isset($objects[$class_name])) {
return $objects[$class_name];
$class_file = DIR_MODELS . "/{$class_name}.inc.php";
if (file_exists($class_file)) {
                                                                                                                                                                                                                                 .       $objects[$class_name] =& new $class_name($param);
                                                                                                                                                                                                                                                                                              ,,                                                                       return null; $class_name , $param and $instaniate , where $param is the parameter of the constructor and $instaniate is optional. Please note that the $objects array in the function is a static variable, that is, the array will not be released when this function is called. The data in this array will be saved the next time this function is called. The advantage of this is that after you instantiate most classes, if you need to call it repeatedly, you can directly return the instance of this class, which avoids repeated calls and improves performance. The code is as follows:
static $objects = array();
if (isset($objects[$class_name])) {
return $objects[$class_name];
}The remaining code is to detect whether there is a file with this class name , if this file is loaded and the class with the specified name is searched, if the class is found, it will be instantiated. This requires that the name of the class in the script must be consistent with the file name of the script. I think this will also help with future code management.
The $instaniate parameter comes into play at this time. This parameter will tell the function to make a mark bit (null) under $objects if it is not found to prevent the function from repeatedly searching for the file name and loading and searching.
         $class_file                            "/{$class_name}.inc.php";                                                                                                                                   
                                                                                                                              return                                                                                                            return else {
              $objects[$class_name] =& new $class_name($param); 
                                                                                                                                   ​tantiate) {
             $objects[$class_name] = null ;
} t Return null;
} The statement:
$ objects [$ class_name] = & new $ class_name ($ param); $class_name is a string variable in the function. The keyword new can dynamically instantiate the class of the specified string (if it exists).See the PHP manual and here for information on this calling method.
The shortcoming of this function is how to consider passing different numbers of parameters to the constructor of each different class. Perhaps it can be implemented using functions such as call_user_func_array, but this approach is very un-Graceful. Some thought is needed here. In fact, the test for the existence of files such as file_exists can be handed over to the __autoload function. However, since other functions such as interface_exists will also call the __autolaod function, for compatibility reasons, only a simple test is done within the function.
PHP5 is more object-oriented than PHP4. I think it's time to update our coding thinking. There is a very good tutorial on PHP5 classes and objects here.

The above introduces some excerpts of the instance PHP instantiation class, including the content of the instance. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template