$this from within a function that inherited from t

2019-07-18 04:07发布

问题:

<?php
class pa{
    private function m(){
        echo 'Parent\'s function';
    }
    public function run(){
        $this->m();
    }
}
class child extends pa{
    public function m(){
        echo 'child\'s function';
    }
}
$obj=new child();
$obj->run();//results: parent's function,why?

code 2

class pa{
    public function m(){//change private to public
        echo 'Parent\'s function';
    }
    public function run(){
        $this->m();
    }
}
class child extends pa{
    public function m(){
        echo 'child\'s function';
    }
}
$obj=new child();
$obj->run();//result:child function

EDIT: Child class extends pa,so it has function run() ready to use,run() function is actually belonged to child class,where $this in this run() function in the subclass should be pointed to the instance of the subclass,but the truth is,it echos 'Parent's function'.

When I change function m() in pa class to public,it printed out "child's function";But why?$this that resides inside a class should all be referred to the object it creates,no matter where the function(which has "$this") comes from.Can any explained to me in a simple way?

edit:

Be more specific,$this in the run() function points two different object in these two cases.

And after waiting for a long time for the answer,it seems no one can explicitly exlpain why in these two scenarios,$this refers two different things.

What is $this refered to in the subclass?

回答1:

$this always refers to the instance,not belonged to the super or the subclass,this instance have both the private and public copy of m function as my first block indicated

    private function m(){
        echo 'Parent\'s function';
    }
    public function m(){
        echo 'Parent\'s function';
    }

But what factor would actually decide which m() function will be executed?This depends on where the function is called,as in my first code block,m() is called in the superclass,that means it has a priority for the m() in the super class to be called,so the result will be pa's function,and in the second code block,since in the inherited class the m() function is overrided so there's only on m() function in the instance,which means it will always calls the m() function in the child class.

The commentors above seems to either missing the point of what I'm asking or do not fully unstand the basic knowledge of the differeces between a class and an instance,beginners always have confusions about these concepts,and I'm here to clarify the differences,and also a rule are worth noted,as $this will first find the function declared where the $this is used,and then looking for the inherited class.So,if in the first code block,move the function run() to the child class as :

<?php
class pa{
    private function m(){
        echo 'Parent\'s function';
    }
}
class child extends pa{
    public function m(){
        echo 'child\'s function';
    }
    public function run(){
        $this->m();
    }
}
$obj=new child();
$obj->run();//results: child function

The result is clear,instance of the child class also contains two m() functions but since m() is called within the child scope,it will use child m() function,nothing to do with private or public