Reputation: 11830
I have been programming in C++ but have moved to PHP for a while it seems like polymorphism is different in PHP. I know that in PHP, all functions that are not private (i.e. public/protected) are in fact also virtual. Here is my child class definition
class Child extends Parent {
public function foo() {
}
}
then my Parent looks like
class Parent {
public function foo() {
}
}
I want my child class to use all of the parent's code apart from the foo()
function - I want the child to use its own foo()
function.
The issue is that the parent's foo()
is still called. Interestingly, my IDE (NetBeans) says that it is getting overridden correctly.
I am using $this->foo()
in the parent and child.. this should be ok yes? It just says 'execute the function foo for the current object'.. maybe I'm going wrong here?
Upvotes: 0
Views: 635
Reputation: 17000
just tried:
class Child1 extends Parent1 {
public function foo() {
echo 'c';
}
}
class Parent1 {
public function foo() {
echo 'p';
}
}
$class = new Child1;
$class->foo();
prints "c"
You doing everithing right.
Upvotes: 2
Reputation: 160863
Unless you call parent::foo()
, the parent's foo()
will not be called.
Upvotes: 3