Fatal error: Using $this when not in object context

元气小坏坏 提交于 2019-11-26 08:35:23

问题


here is the part if having error.

Fatal error: Using $this when not in object context in /pb_events.php on line 6

line 6 is: $jpp = $this->vars->data[\"jpp\"];

function DoEvents($this) {

    global $_CONF, $_PAGE, $_TSM , $base;

    $jpp = $this->vars->data[\"jpp\"];

    $cache[\"departments\"] = $this->db->QFetchRowArray(\"SELECT * FROM {$this->tables[job_departments]}\");
    $cache[\"locations\"] = $this->db->QFetchRowArray(\"SELECT * FROM {$this->tables[job_location]}\");
    $cache[\"names\"] = $this->db->QFetchRowArray(\"SELECT * FROM {$this->tables[job_names]}\");
    $cache[\"categories\"] = $this->db->QFetchRowArray(\"SELECT * FROM {$this->tables[job_categories]}\");

Thanks a lot! appreciate!


回答1:


$this only makes sense in methods, not in functions

this is ok

class Foo {
     function bar() {
          $this->...

this is not

function some() {
    $this->

// edit: didn't notice he passes "$this" as parameter

advice: simply replace "$this" with "$somethingElse"




回答2:


You cannot pass $this to a procedural function. $this is a reserved variable.




回答3:


As per my comments. You want to use $this as passed variable and php doesn't allow it outside class methods body.

function DoEvents($obj) {

    global $_CONF, $_PAGE, $_TSM , $base;

    $jpp = $obj->vars->data["jpp"];

    $cache["departments"] = $obj->db->QFetchRowArray("SELECT * FROM {$obj->tables[job_departments]}");
    $cache["locations"] = $obj->db->QFetchRowArray("SELECT * FROM {$obj->tables[job_location]}");
    $cache["names"] = $obj->db->QFetchRowArray("SELECT * FROM {$obj->tables[job_names]}");
    $cache["categories"] = $obj->db->QFetchRowArray("SELECT * FROM {$obj->tables[job_categories]}");



回答4:


You have to make the object first.

   $object=new Myobject;
   DoEvents($object);


来源:https://stackoverflow.com/questions/1643931/fatal-error-using-this-when-not-in-object-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!