Monday, November 26. 2007Late Static Binding - Changes to parentTrackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
Here is some ugly workaround in PHP.
class O { final protected static function parent( $foo ) { // getting method info $c = new ReflectionClass( get_called_class() ); $m = new ReflectionMethod( $c->getParentClass()->getName(), $foo ); // getting method location $start = $m->getStartLine(); $end = $m->getEndLine(); // gettig a code - it will fail for oneliner $code = trim( implode( "\n", array_slice( file( $m->getFileName() ), $start, $end - $start ) ) ); // checking if there is block begin at the beggining if ( $code[0] != '{' ) $code = '{' . $code; // managing args $func_get_args = func_get_args(); unset( $func_get_args[0] ); $func_get_args = array_values( $func_get_args ); foreach( $m->getParameters() as $key => $val ) { if ( isset( $func_get_args[$key] ) ) ${$val->getName()} = $func_get_args[$key]; } // running code eval( $code ); } } class A extends O { static public $x = 'aaa'; public static function test( $a = 'aa', $x = 'sas') { echo get_called_class(); // works as supposed! writes B for B :: test(); echo "\n\n"; var_dump( func_get_args() ); // wrong.. first argument is method name! echo "\n\n"; var_dump( $func_get_args ); // correct! echo static :: $x; // ok // echo self :: $x; // not ok; (wants O :: $x) echo A :: $x; // ok } } class B extends A { static public $x = 'bbb'; public static function test() { // code of test() ran as parent(); but sees the correct class name // executed almost like parent :: test( 'a', new O() ); but in B-class context static :: parent( 'test', 'a', new O() ); } } B::test(); //outputs B |
Paying for the SiteQuicksearchCategoriesDaily Readsadam trachtenberg
Ben Ramsey Chris Shiflett Dynamically Typed (Harry Fuecks) george schlossnagle John Coggeshall John Lim Marco Tabini PHP Application Tools PHPCommunity.org Planet PHP Sebastian Bergmann Sklar The PHP WTF Tobias Schlitt Tom Sommer Wez Furlong |
