php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33396 Scope Resolution Operator usage seems flawed
Submitted: 2005-06-19 05:42 UTC Modified: 2010-12-20 15:46 UTC
Votes:11
Avg. Score:4.7 ± 0.6
Reproduced:11 of 11 (100.0%)
Same Version:7 (63.6%)
Same OS:8 (72.7%)
From: gabriel at helicoid dot net Assigned: jani (profile)
Status: Closed Package: Class/Object related
PHP Version: 4.3.11 OS: Any
Private report: No CVE-ID: None
 [2005-06-19 05:42 UTC] gabriel at helicoid dot net
Description:
------------
I believe there is a flaw in how the scope resolution operator works.

You can use a variable containing the name of the method you want to call on the right hand side of the operator, which works fine. However, if you try to have the class name on the left hand side in a variable, you get a parse error:

Parse error: parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM

I dare say that having the class name in a variable is actually more useful than having the method name in a variable. You can already have the class name in a variable if you're using the 'new' keyword, as my example code shows, so the operation of the scope resolution operator doesn't seem very consistent with this, which it should be.

A work around would be to actually instantiate an object from the class, as my example code shows, however I don't think this is a particularly good solution to this problem. eval()ing a section of code with the class name as a variable would also work, but again, I don't think this is a good solution either.

I don't _think_ allowing the scope resolution operator to operate in this manner would break any existing scripts either, but I may be wrong.

Reproduce code:
---------------
class TestOne {

        function testMethod($num)
        {
                echo "In testMethod - num is $num\n";
        }

}

$method = 'testMethod';
$class = 'TestOne';

TestOne::$method('3');

//$class::testMethod('3'); // This doesn't work, and I believe it should.

$obj =& new $class;
$obj->testMethod('4');

Expected result:
----------------
I expect $class::testMethod('3') to really evaluate to TestOne::testMethod('3')

Actual result:
--------------
Parse error: parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-19 09:52 UTC] tony2001@php.net
You can use call_user_func() while this is not implemented.
 [2005-06-19 10:35 UTC] gabriel at helicoid dot net
<?php
class TestOne {

        var $number;

        function TestOne($somenum)
        {
                $this->number = $somenum;
                TestTwo::testFunc();
        }

}

class TestTwo {

        function testFunc()
        {
                echo "{$this->number}\n";
        }

}

$obj = new TestOne(3);
?>

This kind of functionality can't be replicated using call_user_func as a workaround.
 [2005-06-20 00:11 UTC] php at taupehat dot com
Not only is the cause of the error a bit >odd< but the error message itself is pretty opaque.  Perhaps it could be switched to T_DOUBLE_COLON?  Yeah, it was a funny joke, but php has kind of reached beyond its roots now and ought to start to shed that sort of silliness.
 [2010-12-20 15:46 UTC] jani@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: Class/Object related -Assigned To: +Assigned To: jani
 [2010-12-20 15:46 UTC] jani@php.net
Works in 5.3.4 at least. Some warnings though, but it does work. :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 15 19:01:34 2024 UTC