0

How do I use $this->var1 inside a method that is called from a static method? I have this method:

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;
}

And I want to call this method:

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']);
    }

}

But I am getting this error:

Error Using $this when not in object context (line 31

Line 31, which is: $api = new MCAPI($this->apiKey);

So how can I fix this, work around this... I am really stuck on this

Thanks in advance!

1
  • Fuel seems to be full of statics, smells!
    – markus
    Commented Dec 5, 2012 at 11:30

2 Answers 2

7

Static Methods don't have any Object associated with them, hence there is no $this reference available inside a static method. However you can declare your variable as static and use it directly without $this reference.

4

Trimbitas is correct, you need

self::$apiKey

$this only works in instantiated objects, not in static class functions.

Not the answer you're looking for? Browse other questions tagged or ask your own question.