|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72903 Spec does not allow $objInstance->memberObj::$staticVar syntax
Submitted: 2016-08-19 22:51 UTC Modified: 2017-09-28 16:04 UTC
From: dliebner at gmail dot com Assigned: nikic (profile)
Status: Closed Package: PHP Language Specification
PHP Version: 7.0 OS:
Private report: No CVE-ID: None
 [2016-08-19 22:51 UTC] dliebner at gmail dot com
From manual page:

Code sample below yields 'unexpected T_PAAMAYIM_NEKUDOTAYIM' syntax error.

Test script:

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:

Actual result:
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting ',' or ';' in __FILE__ on line 25


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-20 02:58 UTC]
-Summary: $objInstance->memberObj::$staticVar yields syntax error +Summary: Spec does not allow $objInstance->memberObj::$staticVar syntax -Operating System: CentOS +Operating System: -PHP Version: 5.6.25 +PHP Version: 7.0
 [2016-08-20 02:58 UTC]
PHP 5.6 does not support that syntax, and I don't think {}s can help with it. PHP 7 does.

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.

 [2016-08-21 04:40 UTC]
-Type: Bug +Type: Feature/Change Request -Package: PHP Language Specification +Package: Scripting Engine problem
 [2017-09-28 16:04 UTC]
-Status: Open +Status: Closed -Type: Feature/Change Request +Type: Bug -Package: Scripting Engine problem +Package: PHP Language Specification -Assigned To: +Assigned To: nikic
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Mon Oct 02 13:01:26 2023 UTC