Calling closure assigned to object property directly

前端 未结 12 2355
孤街浪徒
孤街浪徒 2020-11-22 14:02

I would like to be able to call a closure that I assign to an object\'s property directly without reassigning the closure to a variable and then calling it. Is this possible

12条回答
  •  礼貌的吻别
    2020-11-22 15:03

    Well, if you really insist. Another workaround would be:

    $obj = new ArrayObject(array(),2);
    
    $obj->callback = function() {
        print "HelloWorld!";
    };
    
    $obj['callback']();
    

    But that's not the nicest syntax.

    However, the PHP parser always treats T_OBJECT_OPERATOR, IDENTIFIER, ( as method call. There seems to be no workaround for making -> bypass the method table and access the attributes instead.

提交回复
热议问题