从调用对象数组变量静态方法 [英] Calling static method from object array variable

查看:148
本文介绍了从调用对象数组变量静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中可以调用类的从一个对象实例静态方法(它包含在数组)是这样的:

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

$myArray['instanceOfMyClass']::staticMethod(); // works

但由于某些原因,当我使用 $这种变量,我得到一个解析错误。例如:

But for some reason when I use the $this variable, I get a parsing error. E.g:

$this->myArray['instanceOfMyClass']::staticMethod(); // PARSING ERROR

只是为了说明我的意思:

Just to illustrate what I mean:

class MyClass{
    public static function staticMethod(){ echo "staticMethod called\n"; }
}

$myArray = array();
$myArray['instanceOfMyClass'] = new MyClass;
$myArray['instanceOfMyClass']::staticMethod(); // works

class RunCode
{
    private $myArray;

    public function __construct(){
        $this->myArray = array();
        $this->myArray['instanceOfMyClass'] = new MyClass;
        $this->myArray['instanceOfMyClass']::staticMethod(); // PARSING ERROR
    }
}

new RunCode;

如何解决这个任何想法?

Any ideas on how to get around this?

推荐答案

您实际上可以用 - >调用静态方法:

You actually can use "->" to call static method:

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

这篇关于从调用对象数组变量静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆