Calling static method from object array variable

前端 未结 3 1544
后悔当初
后悔当初 2021-01-07 23:27

In PHP you can call a class\'s static method from an object instance (which is contained in an array) like this:

$myArray[\'instanceOfMyClass\']::staticMetho         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-08 00:13

    You will have to break up the one liner using a temporary variable, e.g.

    $inst = $this->myArray['instanceOfMyClass'];
    $inst::staticMethod()
    

    This is one of many cases where PHP's compiler is not clever enough to understand nested expressions. The PHP devs have been improving this recently but there is still work to do.

提交回复
热议问题