Difference between revisions of "PHP OO problem"
From Organic Design wiki
(the question) |
m |
||
Line 29: | Line 29: | ||
But the ''Y'' method is only ever called via the ''self::Y'' statement in the ''X'' method, so when we call ''Bar::X()'' it's actually ''Foo::Y'' that executes, because ''X'' only ever executes within the context of ''Foo'' since we haven't overridden the ''X'' method with a definition in the ''Bar'' class. | But the ''Y'' method is only ever called via the ''self::Y'' statement in the ''X'' method, so when we call ''Bar::X()'' it's actually ''Foo::Y'' that executes, because ''X'' only ever executes within the context of ''Foo'' since we haven't overridden the ''X'' method with a definition in the ''Bar'' class. | ||
− | So the question is: is there a way to define a ''Bar::X'' method that | + | '''So the question is:''' |
+ | |||
+ | :is there a way to define a ''Bar::X'' method that calls ''Foo:X'' while maintaining the context of ''Bar'' (i.e. the value of ''self'' is "Bar" during execution of ''Foo::X''? |
Revision as of 23:40, 29 July 2013
Take the following example class Foo which defines a static method called X that statically calls another of it's method's called Y using self::Y() as follows.
Now lets say that Foo is part of a core library that we don't have commit access to and we want to make a modified version of the functionality via a sub-class of Foo called Bar which overrides the Y method as follows.
But the Y method is only ever called via the self::Y statement in the X method, so when we call Bar::X() it's actually Foo::Y that executes, because X only ever executes within the context of Foo since we haven't overridden the X method with a definition in the Bar class.
So the question is:
- is there a way to define a Bar::X method that calls Foo:X while maintaining the context of Bar (i.e. the value of self is "Bar" during execution of Foo::X?