php - Return reference to static variable from __callStatic? -
i'm trying find workaround static variables not being copied on extending classes (which doesn't play nicely late static binding), here thought might work, gives me "php fatal error: can't use function return value in write context" :
<?php class person { protected static $tlsb_names = ['name']; protected static $tlsb_vars = []; public static function & __callstatic($method,$args) { echo "call static " . $method . " on " . get_called_class() . "\n"; if(in_array($method,static::$tlsb_names)) { if(!array_key_exists(get_called_class(),static::$tlsb_vars)) { static::$tlsb_vars[get_called_class()] = []; } if(!array_key_exists($method, static::$tlsb_vars[get_called_class()])) { echo "set var $method " . get_called_class() . "\n"; static::$tlsb_vars[get_called_class()] = null; } return static::$tlsb_vars[get_called_class()][$method]; } } public static function show_name() { static::name() . "\n"; } public static function call_me_al() { static::name() = "al"; } public static function call_me_joe() { static::name() = "joe"; } } class al extends person{} class joe extends person{} al::call_me_al(); joe::call_me_joe(); al::show_name(); joe::show_name();
the problematic part lines :
public static function call_me_al() { static::name() = "al"; }
apparently compile-time error since non of echo's run.
what doing wrong here?
the following line of code wrong:
public static function & __callstatic($method,$args)
you need match the definition of __callstatic
functiondocs , without return reference:
public static function __callstatic($name, $arguments)
so try achieve not possible.
and other problem circle around should able solve late static binding (lsb)docs.
also keep in mind magic hard debug, step-debugger ready , step through application can better understand happen. debugger in php called xdebug, php ides , editors support it.
Comments
Post a Comment