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. Please look at the following code below, 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)) {
require_once $class_file;
if (!class_exists($class_name)) {
return false;
} else {
$objects[$class_name] =& new $class_name($param);
return $objects[$class_name] ;
Return null;
}
}The function has only three parameters, namely $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 other continuing code is to detect whether there is a file with this class name. If so, load this file and look for the class with the specified name. 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 avoid the function from repeatedly searching for file names and repeatedly loading and searching. .
$class_file = DIR_MODELS . "/{$class_name}.inc.php";
if (file_exists($class_file)) {
require_once $class_file;
if ( !class_exists($class_name)) {
return false;
} else {
$objects[$class_name] =& new $class_name($param);
return $objects[$class_name];
Return null;
}The statement :
$objects[$class_name] =& new $class_name($param); You can review it multiple times. $class_name is a string variable in the function. The keyword new can dynamically instantiate the class of the specified string (if it exists).For information on this calling method, see the PHP manual and here.
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.