[UPHPU] Polymorphism of Static Methods?
Jacob Fugal
lukfugl at gmail.com
Thu Jul 7 22:35:01 MDT 2005
On 7/7/05, Jacob Fugal <lukfugl at gmail.com> wrote:
> I have class A and class B which extends A. A has two static methods:
> foo() and bar(). Method foo calls method bar as part of its execution.
> Now, B inherits method foo as is, but overrides method bar. In code:
>
> class A {
> public static function foo() {
> print(self::bar() . "\n");
> }
> protected static function bar() {
> return "Hello, world!";
> }
> }
>
> class B extends A {
> protected static function bar() {
> return "Hello, Johnny!";
> }
> }
>
> Given that code, my *expectation* is that calling B::foo() will echo
> "Hello, Johnny!". However, the *actual* result is that "Hello, world!"
> is echoed. That is, B::foo() called A::bar() internally, not B::bar().
> Is there a way around this?
On 7/7/05, Tyler Gee <geekout at gmail.com> wrote:
> Check out this, it may help:
>
> http://us2.php.net/manual/en/language.oop.php#33092
Thanks, Tyler. That does what I need. Pasting the relevant portion for
those interested:
<quote>
class baseclass {
function static1() {return "bla";}
function static2() {return call_user_func(array(__CLASS__,'static1'));}
}
</quote>
So I can go on with my work. However, as an aside, I question why this
kludge is necessary. If the __CLASS__ the method is actually bound to
on execution is readily available, making this kludge possible, why
isn't the default action for self::static1() to use that class rather
than the class the method is defined in? I'm guessing there must be
some rationale behind it, but I can't see it. Maybe I'll get on #php
sometime and ask.
Thanks again for the help...
Jacob Fugal
More information about the UPHPU
mailing list