Calling static method from object array variable

前端 未结 3 1529
后悔当初
后悔当初 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条回答
  •  北海茫月
    2021-01-08 00:13

    This is a really interesting problem, it may even be a bug in PHP itself.

    For a work around, use the KISS principle.

    class RunCode
    {
        private $myArray;
    
        public function __construct(){
            $this->myArray = array();
            $this->myArray['instanceOfMyClass'] = new MyClass;
    
            $instance = $this->myArray['instanceOfMyClass']
            $instance::staticMethod();
        }
    }
    

    Hope this helps!

提交回复
热议问题