Req #63789 [Com]: Parse error on $this->otherInstance::method()

From: Date: Wed, 26 Dec 2012 22:45:00 +0000
Subject: Req #63789 [Com]: Parse error on $this->otherInstance::method()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-175611@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63789&edit=1

 ID:                 63789
 Comment by:         eleni dot fra at gmail dot com
 Reported by:        Alex at phpguide dot co dot il
 Summary:            Parse error on $this->otherInstance::method()
 Status:             Assigned
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 PHP Version:        5.4.9
 Assigned To:        laruence
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2012-12-18 02:49:35] 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 00:35:31] 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-17 14:40:29] 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);



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=63789&edit=1


Thread (5 messages)

« previous php.bugs (#175611) next »