从父类继承的函数中的$ this仍然指向php中的父对象? [英] $this from within a function that inherited from the parent class still points to the parent object in php?

查看:147
本文介绍了从父类继承的函数中的$ this仍然指向php中的父对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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?


代码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

子类扩展了pa,因此它具有可随时使用的功能run(),run()函数实际上属于子类,其中子类中此run()函数中的$ this应该指向子类的实例,但是事实是,它呼应父母的功能".

当我将pa类中的函数m()公开更改时,它打印出子函数";但是为什么呢?驻留在类中的$ this应该全部都引用到它创建的对象,无论该函数在哪里(

更具体地说,在这两种情况下,run()函数中的$ this指向两个不同的对象.

在等待了很长一段时间后,似乎没有人可以明确地解释为什么在这两种情况下,$指的是两种不同的事物.

子类中的$ this是什么?

解决方案

$ this始终引用该实例,不属于父类或子类,该实例同时具有m函数的私有副本和公共副本作为我的第一个块已指示

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

但是究竟是什么因素决定执行哪个m()函数呢?这取决于该函数的调用位置,例如在我的第一个代码块中,在超类中调用m()意味着它具有优先级要调用的超类中的m(),因此结果将是pa的函数,在第二个代码块中,由于在继承的类中m()函数被覆盖,因此实例中仅存在m()函数,这意味着它将始终在子类中调用m()函数.

上面的评论者似乎要么漏掉了我要问的内容,要么没有完全理解类与实例之间的差异的基本知识,初学者对这些概念总是感到困惑,我在这里弄清楚差异,还有一条值得注意的规则,因为$ this首先会找到声明使用$ this的函数,然后寻找继承的类.因此,如果在第一个代码块中,请移动函数run( )作为子类:

<?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

结果很明显,子类的实例也包含两个m()函数,但是由于m()是在子作用域内调用的,因此它将使用子m()函数,与私有或公共无关.

<?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?

解决方案

$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

这篇关于从父类继承的函数中的$ this仍然指向php中的父对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆