Look at the introduction of Datastore, and began to use the MVC way to write PHP, so want to take PHP for Redis to write a model, you can achieve some of the basic functions of datastore ... So I came across a question-.-
PHP __class__ This kind of thing is statically bound, if the subclass is no longer overloaded, then inherit the parent class method is still the name of the parent class rather than the name of the subclass. Like what:
Class a{
function __construct () {
Echo __class__;
}
static function name () {
Echo __class__;
}
}
Class B extends a{}
At this point, either instantiating B or calling the static method directly, the echo will be a. The qeephp is a subclass overload to solve the problem, but in this case no new subclass will have to call the corresponding method of class name overload side ... This is a PHP flaw in oop, try to try Python does not have this problem.
Google's. Found two functions Get_class () and Get_called_class (). Get_class () is used for instance invocations, and adding parameters ($this) resolves the problem of subclass inheritance calls, while Get_called_class () is used for static method calls, but ... This thing only after PHP 5.3 .... 5.3 or more distant things ... OK 5.2 can manually implement this function before: see http://php.net/manual/en/function.get-called-class.php under the master added a few 5.3 before the implementation of the way.
if (!function_exists (' Get_called_class ')) {
Class Class_tools
{
private static $i = 0;
private static $FL = null;
public static function Get_called_class ()
{
$BT = Debug_backtrace ();
Use the Call_user_func or Call_user_func_array function to invoke the class method, handling the following
if (Array_key_exists (3, $BT)
&& array_key_exists (' function ', $BT [3])
&& In_array ($bt [3][' function '], array (' Call_user_func ', ' Call_user_func_array '))
) {
If the argument is an array
if (Is_array ($bt [3][' args '][0])) {
$toret = $bt [3][' args '][0][0];
return $toret;
}else if (is_string ($bt [3][' args '][0])} {//If the argument is a string
If it is a string and the string contains:: symbol, the argument type is considered correct, and the class name is evaluated and returned
if (False!== strpos ($bt [3][' args '][0], ':: ')} {
$toret = Explode (':: ', $bt [3][' args '][0]);
return $toret [0];
}
}
}
Call a class method using the normal path, such as: A::make ()
if (self:: $FL = = $BT [2][' file ']. $BT [2][' line ']) {
Self:: $i + +;
} else {
Self:: $i = 0;
Self:: $FL = $bt [2][' file ']. $BT [2][' line '];
}
$lines = File ($bt [2][' file ']);
Preg_match_all ('
/([a-za-z0-9\_]+):: '. $BT [2][' function ']. ' /',
$lines [$BT [2][' line ']-1],
$matches
);
return $matches [1][self:: $i];
}
}
function Get_called_class ()
{
return Class_tools::get_called_class ();
}
}
So now you can change the example:
Class a{
function __construct () {
echo Get_class ($this);
}
static function name () {
Echo Get_called_class ();
}
}
Class B extends a{}
This will let B directly inherit the method of getting the current class name.