0

I am new to working with php classes, so bear with me.

I am getting this error:

"Fatal error: Using $this when not in object context in..."

I am attempting to call a function inside the same class. I am able to call the same function from another class so I am confused.

Here is the snippet from the class:

    public function listLocations() {

    // get all the locations
    $listLocations = self::getLocations();

    echo '<div class="wrap"><div id="icon-options-general" class="icon32"><br></div><h2>Locations Class</h2></div>';

    foreach ($listLocations as $data) {

        echo 'Location - '.$data->location.'<br>';

    }

}


public function getLocations(){

    $Locations = $this->db->get_results("select location_id, location FROM {$this->soups_locations_db}");

    return $Locations;

}

This is inside the Class Foo_Locations

I am calling this same function using this snippet from another class. I get the results that I am looking for without error so I am confused.

$myLocations = count(Foo_Locations::getLocations());

The error is pointing to this line in the Foo_Locations Class

    $Locations = $this->db->get_results("select location_id, location FROM {$this->soups_locations_db}");

But I think its related to this line:

$listLocations = self::getLocations();

This community's help is always greatly appreciated. Thanks in advance

akaur
  • 389
  • 1
  • 6
  • 22
Mike
  • 11
  • 2

1 Answers1

1

The problem is that you are calling getLocations statically using self::getLocations();

$this->getLocations() allows you to use the instantiated class to call getLocations().

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116