In PHP, I'm trying to reference a method defined in an object's parent class, from a method inherited from the object's parent class. Here's the code:
class base_class {
function do_something() {
print "base_class::do_something()\n";
}
function inherit_this() {
parent::do_something();
}
}
class middle_class extends base_class {
function do_something() {
print "middle_class::do_something()\n";
}
}
class top_class extends middle_class {
function do_something() {
print "top_class::do_something()\n";
$this->inherit_this();
}
}
$obj = new top_class;
$obj->do_something();
The problem is that parent::do_something() in inherit_this() tries to find the parent class of base_class, not the parent of the object's actual class, and the example above throws an error. Is there something I can write instead of parent::do_something() that would call middle_class::do_something(), and that would still work even in classes that extend (say) top_class?
I'll start by saying "Thank you" to grrbrr404. He gave me some ideas and got me started in the right direction.
The solution I finally settled on was the following:
It's not pretty (I particularly hate calling
debug_backtrace()
for this), but it keeps the object context set to $this, and handles the case where the method is called from a method somewhere in the middle of the object hierarchy.For those who found my example confusing and/or wanted to know "Why would you want to do this?" I apologize, and provide the following additional example, which is hopefully more illustrative and closer to the original problem. It is considerably longer, but hopefully shows why I care about keeping $this set properly, and also shows why I can't hard-code any particular class name or use $this->method(). Avoiding infinite loops is always a priority with me. :-)
I'm trying to avoid duplication of the "checkType()" function, yet still provide correct behavior even when the hierarchy gets arbitrarily large.
More elegant solutions are, of course, most welcome.
Im not quite sure I understands why you want to do like this. But I guess you cannot do it. I understand that you will be able to make a middle_class2 and be able inherit from that, and then it would be middle_class2 instead of middle_class's dosomething you call?!
So I guess you'll need to create the
in the middle_class.
I thought about a get_class($this)::parent::do_something().. but that didn't work.
Just to be sure.. You want to call middle_class::do_something() right??
To get it work you can modify your base_class like this:
Then your top_clas will call inherit_this() of your base class, but there will be a recursion: do_something() of top_class calls $this->inherit_this(), and in base_class you call again $this->do_something() (in your base class $this will reference to your top_class). Because of that, you will call inherit_this() over and over again.
You should rename the methods to prevent that.
Update
If you want that base_class inherit_this() prints "base_class::do_something" you could modify your base_class like this:
}
In this case you make a static call to the base_class method do_something(). The output is
top_class::do_something() base_class::do_something()
Update 2
Regarding to your comment you can modify your base_class like this:
You get the parrent class of $this and then call the method. Output will be:
top_class::do_something() middle_class::do_something()