[UPHPU] Polymorphism of Static Methods?
Mac Newbold
mac at macnewbold.com
Fri Jul 8 12:34:41 MDT 2005
Today at 11:43am, Fred Larsen said:
> This works but probably not a good solution.
It may be the best solution available at this point. It may not be pretty,
and it may require defining a stub foo() in every class, but hey, it works
right.
There's also something listed in the comments of the PHP5 OOP Static page
on php.net that shows a way to do this without adding the parameter, by
using a backtrace to look up the class that was called, but it is probably
more fragile.
Mac
> <?
> 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!
--
Mac Newbold MNE - Mac Newbold Enterprises, LLC
mac at macnewbold.com http://www.macnewbold.com/
More information about the UPHPU
mailing list