PHP uses reflection to truly implement multiple inheritance (non-interface simulation)

Source: Internet
Author: User
yesterday I was writing PHP program, accidentally found in PHP:: operator is very powerful, not only used in access to Parent,sel, static member properties, constants above, in fact, his function is powerful



This symbol is actually called the range parser in PHP, which is the official explanation of PHP php.net/manual/zh/language.oop5.paamayim-nekudotayim.php. But the official Chinese part is just a simple introduction.

Access to Parent,sel, static member properties, constants, and the following English part, perhaps because we hang on the English bad reason, did not look down, implementation, the following there is a strong feature, we all did not see.
What is called the range operator, perhaps a lot of people did not seriously to analyze, these words, according to my two years of programming understanding, I think it should be defined in the class member properties and member methods in the scope, feel a bit similar to the sense of namespace.
and Self::method () actually refers to the method that invokes the current scope, that parent naturally refers to the scope of the parent class, which is why we can still use Parent::method () for the method of overriding the parent class in the subclass. It is also important to note that self always points to the class itself where the declaration is located and that parent always points to the class that declares the location, and is not affected by the inheritance context.
These are probably some people already know, above said:: In PHP called the scope parsing operator, I said above since, both parent and self can point to a synonym for a class scope, then imagine, we can use the class name point to a range, the answer is certainly possible. We can do this.

Copy Code
    1. Class a{
    2. Public Function demo () {
    3. Echo ' abc ';
    4. }
    5. }
    6. A::d emo ();
I don't know, we found out. Inside the demo method is not static method, but I can access, it is because of a:: Pointing to the class range, so you can access the time surface of the demo method, PHP has such a sample code, in the above I sent the link, we can look carefully.

We must also wonder how this has to do with more inheritance, don't worry, impatient to eat hot tofu!!!!!
in fact, this range operator has a very large feature, when a non-static public method is called outside of a class, the $this pointer inside the method points to the object itself (the host object) that he is currently running, and when a class is instantiated, the $this object itself is always pointed to by the method. Why, because this method is executed by this object invocation, so this object is his host object. And just as we call this method directly in global tuning, because this method is not in any object at run time, there is no host object, so if you write $this in the method, you will report a fatal error. How can you have a host object and then call it directly. Look closely at what I have just said that the classmate may have noticed that I have just mentioned "because this method is executed by this object invocation, so this object is his host object". So in theory, we can change the host object as long as we change the environment in which the method is executed. Now let's do the experiment.


Class a{
Public Function Say_name () {
Echo ' My name is '. $this->name;
}
}


Class b{
Public $name = ' xbs530 ';
Public Function run () {
A::say_name ();
}
}

$o =new B;
$o->run ();
Let's guess what the output is ....
We can do the experiment ...

Because the A::say_name () is called by the object $o, the $this of the method naturally points to the object $o. It's amazing ....

This I have previously sent the PHP scope of the analytic operators are said, good basis for here, the following directly more inherited code, Welcome to spit ....

/*
Multiple inheritance base class
Note: To implement class multiple inheritance, you must first inherit this base class
*/

Class multi_extends{

Public Function __construct () {
$this->_init_extends ();
}

protected function _init_extends () {
if (property_exists ($this, ' _extends '))
{
$extends =& $this->_extends;
foreach ($extends as $class)
{
$this->_extends ($class);
}
}
}

Public Function _extends ($class _name) {
Analysis target
$ref = new Reflectionclass ($class _name);

Inheriting public properties
$property _list= $ref->getproperties (reflectionproperty::is_public);
foreach ($property _list as $property)
{
$property _name= $property->name;
$property _value= $property->getvalue (new $class _name);
if ($property _name=== ' _extends ')
{
foreach ($property _value as $c)
{
$this->_extends[]= $c;
}
}else{
if (!property_exists ($this, $property _name))
{
$this->{$property->name}= $property _value;
}
}
}

Inheriting public methods
$method _list= $ref->getmethods (reflectionmethod::is_public);
foreach ($method _list as $method)
{
$this->_extends_method[$method->name]= $method->class;
}
}

function __call ($m, $a) {
if ($c = $this->_extends_method[$m])
{
Eval ("$c:: $m (". $a '; ");
}
}
}













Header (' Content-type:text/html;charset=utf-8 ');

Class a{
Public $a = 5;

Public Function Say_name () {
Echo ' My name, ' $this->name. ' <br> ';
}
}

Class b{
function Say_age () {
Echo ' I'm this year '. $this->age, ' year old <br> ';
}

}


Class C extends multi_extends{
Public $_extends=array (' A ', ' B ');


Public Function Say_hellow () {
Echo ' Hellow '. $this->name. ' <br> ';
}
}


Class d{
function Say_goodbye () {
echo ' goodbye '. $this->name. ' <br> ';
}

}




Class Persion extends multi_extends{
Public $_extends=array (' C ', ' d ');


Public $name = "Shunbao";
Public $age = "20";



}


Methods after the inheritance is executed
$o =new persion ();
$o->say_name ();
$o->say_age ();
$o->say_hellow ();
$o->say_goodbye ();
There is no understanding of the place can add qq:987188548 mutual discussion, Mail can also: xbs530@yeah.net hehe


Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.