php.net |  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
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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-20 02:58 UTC] requinix@php.net
-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] requinix@php.net
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
 [2016-08-21 04:40 UTC] stas@php.net
-Type: Bug +Type: Feature/Change Request -Package: PHP Language Specification +Package: Scripting Engine problem
 [2017-09-28 16:04 UTC] nikic@php.net
-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-2021 The PHP Group
All rights reserved.
Last updated: Sun Nov 28 09:03:14 2021 UTC