|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2012-12-17 14:40 UTC] Alex at phpguide dot co dot il
 Description:
------------
Calling a static method on an instance of class which is member of another class 
results in parse error.
Test script:
---------------
class Speaker
{
	public static function say($str) { echo $str; }
}
$speaker = new Speaker();
$speaker::say('This works');
class failingToCallSpeaker
{
	private $speaker;
	
	public function __construct(Speaker $s)
	{
		$this->speaker = $s;
	}
	
	public function doesntWork($str)
	{
		$this->speaker::say($str);
		// PHP Parse error:  syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) 
	}
	
	public function works($str)
	{
		$s = & $this->speaker;
		$s::say($str);
	}
}
$dontwork = new failingToCallSpeaker($speaker);
$dontwork->works('hurray');
$dontwork->doesntWork('argh');
Expected result:
----------------
PHP Parse error:  syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) on line 
$this->speaker::say($str);
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 14:00:01 2025 UTC | 
Just wanted to verify that I have noticed a similar behavior when accessing class constants in PHP 5.4.4. When accessing the constant via the class name, or via an object of the class that defines the constant, or via an object returned by a getter of a class that uses the object as a property the constant is properly returned. But when trying to access the constant by using $this->object::const produces a parsing error while assigning the object to a temporary variable and using $var::const works. class one { const ALPHA = 0; function __construct() {} } class two { protected $oo1; public function __construct() { $this->oo1 = new one(); } public function getOne() { return $this->oo1; } public function printAlphaDirectly() { //echo $this->oo1::ALPHA . PHP_EOL; } public function printAlphaTmp() { $tmp = $this->oo1; echo $tmp::ALPHA . PHP_EOL; } } echo one::ALPHA . PHP_EOL; // (Output= 0) It works as expected $o1 = new one(); echo $o1::ALPHA . PHP_EOL; // (Output= 0) It works when passing an object $t1 = new two(); $o2 = $t1->getOne(); echo $o2::ALPHA . PHP_EOL; // (Output= 0) It works when using an object returned from a getter $t1->printAlphaDirectly(); // PHP Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) $t1->printAlphaTmp(); // (Output= 0) It works when using a tmp var to hold the object