PHP extend a class method that is called from another class which extends it and calls the method wi
Posted
by dan.codes
on Stack Overflow
See other posts from Stack Overflow
or by dan.codes
Published on 2010-04-21T21:12:49Z
Indexed on
2010/04/21
22:03 UTC
Read the original article
Hit count: 197
php
I am trying to extend a class and override one of its methods. lets call that class A. My class, class B, is overiding a protected method. Class C extends class A and Class D extends class C. Inside of Class D, the method I am trying to overwrite is also extended here and that calls parent::mymethodimoverriding. That method does not exist in class C so it goes to it in class A. That is the method I am overiding in class B and obviously you can't extend A with B and have those changes show up in D since it does not fall in line with the class hierarchy. I might be wrong, so correct me please.
so if my class b is called and ran then class D gets called it runs the method in A and overwrites what I had set. I am thinking there must be a way to get this to work, I am just missing something.
here is an example, as you can see in class A there is a call to setTitle and it is set to "Example" In my class I set it to "NewExample". My class is getting called before class D so when class D is called it goes back to the parent and sets the title back to "Example"
class A{
protected function _thefunction(){
setTitle("Example");
}
}
class B extends A{
protected function _thefunction(){
My new code here
setTitle("NewExample");
}
}
class C extends A{
nothing that matters in here for what I am doing
}
class D extends C{
protected function _thefunction(){
parent::_thefunction();
additional code here
}
}
© Stack Overflow or respective owner