[UPHPU] Polymorphism of Static Methods?

Fred Larsen fred at bitwyze.com
Fri Jul 8 11:43:20 MDT 2005


On Jul 8, 2005, at 11:00 AM, Jacob Fugal wrote:

> On 7/7/05, Jacob Fugal <lukfugl at gmail.com> wrote:
>
>> <quote>
>> class baseclass {
>>    function static1() {return "bla";}
>>    function static2() {return call_user_func(array 
>> (__CLASS__,'static1'));}
>> }
>> </quote>
>>
>
> Bad news, folks. It looks like this doesn't work after all:
>
> $ cat test.php
> <?
> class A {
>     static function foo() {
>         $bar = call_user_func(array(__CLASS__,'bar'));
>         print($bar . "\n");
>     }
>     static function bar() { return "Hello, world!"; }
> }
>
> class B extends A {
>     static function bar() { return "Hello, Johnny!"; }
> }
>
> A::foo();
> B::foo();
> ?>
>
> $ php test.php
> Content-type: text/html
> X-Powered-By: PHP/5.0.4
>
> Hello, world!
> Hello, world!

This works but probably not a good solution.

<?
class A {
     static function foo($class = __CLASS__) {

         $bar = call_user_func(array($class,'bar'));

         print($bar . "\n");
     }
     static function bar() { return "Hello, world!"; }
}

class B extends A {


     static function foo() {
         parent::foo(__CLASS__);
     }

     static function bar() { return "Hello, Johnny!"; }

}

A::foo();
B::foo();
?>

Content-type: text/html
X-Powered-By: PHP/5.0.4

Hello, world!
Hello, Johnny!






More information about the UPHPU mailing list