Calling a child method from the parent class in PHP
Solution 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.
Solution 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
Solution 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();
Songo
Updated on June 05, 2022Comments
-
Songo almost 2 years
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 fromTheParent
shouldn't$this
called inparse()
be referring to the instance of$child
, sovalidate()
will be visible toparse()
?Note:
After doing some research I found that the solution to this problem would either make thevalidate()
functionprotected
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
methodvalidate()
in the parent and override it in the child (which will be redundant) to the first solution asprotected
methods of a child can be accessed from the parent?!!Can someone please explain how the inheritance works in this case?
-
Songo over 11 yearshmmm so when I call
$child->parse()
in my code it will run the functionparse
in the parent class and$this
inside it will be referring to the parent class instance not the child? -
Namrata Das over 11 yearsThere is only one instance, but from the perspective of the instance.. private methods of parent classes are simply not allowed to be called. This is the exact purpose and difference between
private
,protected
andpublic
. -
pythonian29033 over 10 yearspity the guy who actually understands OOP a level beyond the rest is overlooked like this. I would've given the abstract function answer aswell
-
Hector Ordonez over 9 yearsThis is the correct answer. It is not enough to just implement the method in the child class, and crossing fingers in the hope that it will always be so. Ensuring that the child MUST define the method is the way to go!