dtgr3392 2016-02-19 20:33
浏览 23
已采纳

我使用php类卡住了这个错误[重复]

This question already has an answer here:

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

</div>
  • 写回答

1条回答 默认 最新

  • doulu3399 2016-02-19 20:36
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号