1

Consider this script

class B
{
    static public function hi() { echo "hi\n"; }
}
class A
{
    private $name = 'B';

    public function __construct()
    {
        // This doesn't parse
        // $this->name::hi();

        // But this works fine
        $class = $this->name;
        $class::hi();
    }
}
new A();

Any idea how/if I could get the first example $this->name::hi(); to parse and work?

8
  • 2
    What version of PHP do you have? $class::hi() should work in 5.3. Commented Oct 23, 2014 at 1:36
  • he is saying // But this works fine is working fine whats not working is the other one i think its not clear Commented Oct 23, 2014 at 1:37
  • @Barmar yes, he already said that, what does not work is $this->$name::hi(), looks like it should be $this->name, that's probably the problem. Commented Oct 23, 2014 at 1:37
  • i think its becos $name is just variables you havent assigned the class B to that var yet.. Commented Oct 23, 2014 at 1:39
  • 2
    Is it really so bad to have to create a variable first? Why do people need to turn everything into one-liners? Commented Oct 23, 2014 at 1:42

2 Answers 2

2

The current implementation of Zend PHP parser supports only static method calls that are made directly on a class name or a variable. Here is the grammar:

%token T_PAAMAYIM_NEKUDOTAYIM ":: (T_PAAMAYIM_NEKUDOTAYIM)"

function_call:
    name argument_list
        { $$ = zend_ast_create(ZEND_AST_CALL, $1, $2); }
|   class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
        { $$ = zend_ast_create(ZEND_AST_STATIC_CALL, $1, $3, $4); }
|   variable_class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
        { $$ = zend_ast_create(ZEND_AST_STATIC_CALL, $1, $3, $4); }
|   callable_expr argument_list
        { $$ = zend_ast_create(ZEND_AST_CALL, $1, $2); }

Source: https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L890

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is the kind of answer I was looking for.
-1

Given that in your current circumstances you require a method of the class b within the class a, it seems pretty anti intuitive to approach it this way. Further, the the availability to call a method of another class is simply not there, you would need to use:

call_user_func(array($this->name, 'hi'));

one way is to simply require the B class as a depency of the constructor function of the A class:

class B
{
    static public function hi() { echo "hi\n"; }
}

class A
{
    private $b;

    public function __construct(B $b)
    {

        $b::hi();

    }
}
new A(new B);

This is probably as close to the one liner approach that you're looking for that you can get.

1 Comment

This doesn't really answer my question, and actually changes the semantics anyway b/c I don't have an instance of B, I just want to refer to the class B, specifically through a string stored in an instance variable of another class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.