|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-08-19 22:51 UTC] dliebner at gmail dot com
Description: ------------ --- From manual page: http://www.php.net/language.oop5.static --- Code sample below yields 'unexpected T_PAAMAYIM_NEKUDOTAYIM' syntax error. Test script: --------------- <?php class MyClass { public static $var = 'yay!'; public $childClass; public function __construct() { $this->childClass = new ChildClass(); } } class ChildClass { public static $var = 'yay?'; } $obj = new MyClass(); echo $obj::$var; // works echo $obj->childClass::$var; // syntax error ?> Expected result: ---------------- yay!yay? Actual result: -------------- Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting ',' or ';' in __FILE__ on line 25 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 09:00:02 2025 UTC |
PHP 5.6 does not support that syntax, and I don't think {}s can help with it. PHP 7 does. https://3v4l.org/ad03D Looking at the spec [1], I see two problems with "$obj->childClass::$var": 1. $obj->childClass would be classified as a scope-resolution-qualifer > expression, which "must be a value of type string [...] that contains the name of a class or interface type". This is not true: objects are supported, as seen in class Example { static $foo = "bar"; } $e = new Example(); echo $e::$foo; // bar 2. $var must be a member-selection-designator, however that is defined [2] as either a name (without a '$') or "a value of type string [...] that contains the name of an instance property (without the leading $) [or a method]". Neither of those cover the static property usage (which the prose for the scope-resolution operator calls "otherwise"), thus referencing member-selection-designator is insufficient; I think adding a ":: $ name" covers this case. So I'm repurposing this bug to deal with the spec. [1] https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#scope-resolution-operator [2] https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#member-selection-operator