Calling a child method from the parent class in PHP

安稳与你 提交于 2019-12-08 01:04:34

问题


Having the following class hierarchy:

class TheParent{

    public function parse(){
        $this->validate();
    }

}

class TheChild extends TheParent{

    private function validate(){
        echo 'Valid!!';
    }
}

$child= new TheChild();
$child->parse();

What is the sequence of steps in which this is going to work?

The problem is when I ran that code it gave the following error:

Fatal error: Call to private method TheChild::validate() from context 'TheParent' on line 4

Since TheChild inherits from TheParent shouldn't $this called in parse() be referring to the instance of $child, so validate() will be visible to parse()?

Note:
After doing some research I found that the solution to this problem would either make the validate() function protected according to this comment in the PHP manual, although I don't fully understand why it is working in this case.

The second solution is to create an abstract protected method validate() in the parent and override it in the child (which will be redundant) to the first solution as protected methods of a child can be accessed from the parent?!!

Can someone please explain how the inheritance works in this case?


回答1:


Your idea of inheritence is correct, just not the visibility.

Protected can be used by the class and inherited and parent classes, private can only be used in the actual class it was defined.




回答2:


Other posters already pointed out that the mehods need to be protected in order to access them.

I think you should change one more thing in your code. Your base class parent relies on a method that is defined in a child class. That is bad programming. Change your code like this:

abstract class TheParent{

    public function parse(){
        $this->validate();
    }

    abstract function validate();

}

class TheChild extends TheParent{

    protected function validate(){
        echo 'Valid!!';
    }
}

$child= new TheChild();
$child->parse(); 

creating an abstract function ensures that the child class will definitely have the function validate because all abstract functions of an abstract class must be implemented for inheriting from such a class




回答3:


Private can only be accessed by the class which defines, neither parent nor children classes.

Use protected instead:

class TheParent{

    public function parse(){
        $this->validate();
    }

}

class TheChild extends TheParent{

    protected function validate(){
        echo 'Valid!!';
    }
}

$child= new TheChild();
$child->parse();



回答4:


FROM PHP DOC

Visibility from other objects

Objects of the same type will have access to each others private and protected members even though they are not the same instances. This is because the implementation specific details are already known when inside those objects.

Private can only be accessed by the class which defines or Same object type Example

class TheChild {
    public function parse(TheChild $new) {
        $this->validate();
        $new->validate(); // <------------ Calling Private Method of $new
    }
    private function validate() {
        echo 'Valid!!';
    }
}

$child = new TheChild();
$child->parse(new TheChild());

Output

Valid!!Valid!!



来源:https://stackoverflow.com/questions/12934744/calling-a-child-method-from-the-parent-class-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!