php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63789 Parse error on $this->otherInstance::method()
Submitted: 2012-12-17 14:40 UTC Modified: 2014-10-12 15:13 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: Alex at phpguide dot co dot il Assigned: laruence (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.9 OS:
Private report: No CVE-ID: None
 [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);


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-18 00:35 UTC] aharvey@php.net
-Package: Class/Object related +Package: Scripting Engine problem
 [2012-12-18 00:35 UTC] aharvey@php.net
Update the package — I'm pretty sure this is a limitation of the parser that isn't likely to be fixed any time soon, but hopefully one of the guys who works on the parser can confirm that.
 [2012-12-18 02:49 UTC] laruence@php.net
you don't need always use :: to call static method,  actually,  :: doesn't means 
calling static method all the time.

you could use $this->speaker->say($str).

anyway, I agree we can do some improvement here, assign to myself. I can solve 
this in the mean time of implement the instance function call FR

thanks
 [2012-12-18 02:49 UTC] laruence@php.net
-Type: Bug +Type: Feature/Change Request -Assigned To: +Assigned To: laruence
 [2012-12-26 22:45 UTC] eleni dot fra at gmail dot com
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
 [2014-10-12 15:13 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2014-10-12 15:13 UTC] nikic@php.net
Supported in PHP 7.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 08:01:27 2024 UTC