The following is a function that invokes a model (Module). The basic function of this function is to specify the name of a model (which is abstracted to a class), and then it looks for the script instantiation of the class below the model directory to return. One advantage of this approach is that onboarding and instantiation are automatic and you get maximum flexibility. Here's a look at the following code, which is not long and not complex:
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];
}
} else {
if ($instantiate) {
$objects [$class _name] = null;
}
return null;
}
The} function has only three parameters, namely $class _name, $param, and $instaniate, where $param is the parameter of the constructor, $instaniate is optional. Note that the $objects array in the function is a static variable, that is, the array is not freed when the function is called, and the data for this array is saved the next time the function is called. The advantage of this is that most of the classes can be instantiated later, such as the need to repeat the call to return the instance of the class directly, to avoid repeated calls, improve performance. The code is as follows:
Static $objects = Array ();
if (Isset ($objects [$class _name])) {
return $objects [$class _name];
The other continuation code is to detect if there is a file of this class name, and if there is a class that loads the file and looks for the specified name, it is instantiated after the class is found. This requires that the name of the class in the script be consistent with the file name of the script. I think it's also good for future code management.
$instaniate parameter is effective at this time, this parameter tells the function to do a mark bit below the $objects if it is not found (null) to avoid the function and repeat the search for the file name and repeat 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];
}
} else {
if ($instantiate) {
$objects [$class _name] = null;
}
return null;
Where 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 one exists. Refer to the PHP manual and here for this call method.
The disadvantage of this function is how to consider passing different number of arguments to each of the different constructors of the class. It may be possible to use functions such as Call_user_func_array, but this is not a very good way to do Grace. It needs to be hammered out here. In fact, file_exists files such as the existence of the test can be handed to __autoload function processing, but because other functions such as interface_exists will also invoke the __AUTOLAOD function, for compatibility considerations, so only in the function to do a simple test.
PHP5 is more object-oriented relative to PHP4. I think it's time for us to update our coding ideas. For PHP5 classes and objects, here's a very good tutorial.
The above describes the instance of PHP instantiation of a bit of notes, including the content of the example, I hope the PHP tutorial interested in a friend helpful.