在此方法调用的静态方法和方法中使用$ this

Haw*_*iak 0 php static

如何$this->var1在静态方法调用的方法中使用?我有这个方法:

static public function getModuleConfigInputfields(array $data) {
    $fields = new InputfieldWrapper();
    $modules = Wire::getFuel('modules');
    $field = $modules->get("InputfieldText");
    $field->attr('name+id', 'apiKey');
    $field->attr('value', $data['apiKey']);
    $field->label = "API Key (Developer Key)";
    $field->description = 'Enter the API key';
    $fields->append($field);
    $field = $modules->get("InputfieldSelect");
    $field->attr('name+id', 'list_id');
    $mailing_lists = self::get_mc_lists();
    foreach($mailing_lists['data'] as $list)
    {
        $field->addOption($list->list_name, $list->list_id); 
    }
    $field->label = "Mailing list";
    $field->description = 'Choose a mailing list';
    $fields->append($field);
    return $fields;
}
Run Code Online (Sandbox Code Playgroud)

我想称这种方法:

public function get_mc_lists()
{
    $api = new MCAPI($this->apiKey);

    $retval = $api->lists();

    if ($api->errorCode){
        return array('errorcode' => $api->errorCode, 'errormessage' => $api->errorMessage);
    } else {
        return array('data' => $retval['data'], 'total' => $retval['total']);
    }

}
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误:

错误在不在对象上下文中时使用$ this(第31行

第31行,即: $api = new MCAPI($this->apiKey);

那么我该如何解决这个问题呢?解决这个问题......我真的很困惑

提前致谢!

Sou*_*mya 7

静态方法没有任何与之关联的对象,因此$this静态方法中没有可用的引用.但是,您可以将变量声明为静态,并直接使用它而不$this引用.