php 5.5 - Semi-complicated dereferencing in PHP 5.5 -
the general idea so:
- i have class
test - the class has public static property
$commands test::$commandsarray ofkey => callbackpairs- i have key saved in
$cmdkey
all considered, should able write this:
self::$commands[$cmdkey]($argument); however, doing yields:
php notice: undefined variable: $commands
php fatal error: function name must string
i solved issue doing this:
$callback = self::$commands[$cmdkey]; $callback($argument); it's kind of blowback before dereferencing thing in php, though...
am going crazy, or have found bug in php parser?
it looks like
test::$commands[$cmdkey](foo) is interpreted as
test::($commands[$cmdkey])(foo) i.e. first fetch $commands[$cmdkey] contains , use function name in test. note test::$commands[$cmdkey] binds normally.
consider:
class test { public static $commands = array('x' => 'strlen'); public static function other() { print 'here!'; } } $cmdkey = 'x'; $commands = array('x' => 'other'); // * print test::$commands[$cmdkey]('abc'); if comment out * line, you'll get
notice: undefined variable: commands... fatal error: function name must string... sadly, http://www.php.net/manual/en/language.operators.precedence.php lacks :: hard if behaviour intended, it's counter-intuitive.
this phpparser says:
$code = <<<'eof' <?php test::$commands[123](456); ?> eof; $parser = new phpparser\parser(new phpparser\lexer); $stmts = $parser->parse($code); $nodedumper = new phpparser\nodedumper; print $nodedumper->dump($stmts); result:
0: expr_staticcall( class: name( parts: array( 0: test ) ) name: expr_arraydimfetch( var: expr_variable( name: commands ) dim: scalar_lnumber( value: 123 ) ) args: array( 0: arg( value: scalar_lnumber( value: 456 ) byref: false unpack: false ) ) )
Comments
Post a Comment