[UPHPU] Polymorphism of Static Methods?

Tyler Gee geekout at gmail.com
Thu Jul 7 20:52:32 MDT 2005


Check out this, it may help:

http://us2.php.net/manual/en/language.oop.php#33092

On 7/7/05, Jacob Fugal <lukfugl at gmail.com> wrote:
> Ok, I have a question about inheritance and polymorphism with static methods...
> 
> 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?
> 
> I figured self would be bound to the calling class (B) but it appears
> to be bound at compile time instead. Is there some other keyword I
> should use instead of self that will be bound to the calling class?
> 
> Note that these are *static* methods, so using $this won't work. First
> off, since method foo is declared static, trying to use $this raises a
> fatal error. If I remove the static declaration, things work fine for
> the use case $b->foo(), where $b is an instance of B. But the use case
> B::foo() (which is the one I really need) still breaks since $this ==
> NULL under that invocation.
> 
> Any ideas?
> 
> Jacob Fugal
> 
> 
> _______________________________________________
> 
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
>




More information about the UPHPU mailing list