How to obtain subclass names using the PHP inheritance method
Source: Internet
Author: User
After reading the datastore introduction and using MVC to write php, I want to use php to write a model for redis to implement some basic functions of datastore... so I encountered such a problem -. -In php, the _ CLASS is statically bound. if it is no longer overloaded in the subclass, the Synt that inherits the parent CLASS method will take a look at the datastore entry, and started to use MVC to write php, so I want to use php to write a model for redis, which can implement some basic functions of datastore... so I encountered such a problem -.-
In php, the _ CLASS is statically bound. if the subclass is not overloaded, the method inherited from the parent CLASS will still get the name of the parent CLASS rather than the name of the subclass. For example:
Class {
Function _ construct (){
Echo _ CLASS __;
}
Static function name (){
Echo _ CLASS __;
}
}
Class B extends {}
In this case, whether B is instantiated or the static method is directly called, the echo will be. In qeephp, this problem is solved by using subclass overloading. However, if no new subclass is created, you have to reload the method of calling the class name ..... this is a defect of php in oop. I tried python and didn't have this problem.
Google. Find the get_class () and get_called_class () functions (). Get_class () is used for instance calling. adding a parameter ($ this) solves the problem of subclass inheritance calling, while get_called_class () is used for static method calling,... this is only available after php 5.3 .... 5.3 is still far away... fortunately, you can manually implement this function before 5.2: see http://php.net/manual/en/function. below the get-called-class.php is a master added several ways of implementation before 5.3.
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 ();
// Call the class method using the call_user_func or call_user_func_array function.
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 parameter 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 parameter is a string
// If it is a string and the string contains: symbol, it is considered to be the correct parameter type, and the class name is calculated and returned.
If (false! = Strpos ($ bt [3] [args] [0],: :)) {
$ Toret = explode (::, $ bt [3] [args] [0]);
Return $ toret [0];
}
}
}
// Call the class method through A normal method, for example, 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 we can modify the example as follows:
Class {
Function _ construct (){
Echo get_class ($ this );
}
Static function name (){
Echo get_called_class ();
}
}
Class B extends {}
In this way, B can inherit the method to obtain the current class name ~
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service