致命错误:在不在对象上下文中时使用$ this

时间:2009-10-29 13:58:03

标签: php oop fatal-error

这是错误的部分。

  

致命错误:不在时使用$ this   对象上下文   /pb_events.php   在第6行

第6行是:$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]}");

非常感谢!欣赏!

4 个答案:

答案 0 :(得分:14)

$这只在方法中有意义,而不在函数中

这没关系

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

这不是

function some() {
    $this->

//编辑:没有注意到他将“$ this”作为参数

传递

建议:只需将“$ this”替换为“$ somethingElse”

答案 1 :(得分:7)

您无法将$this传递给程序功能。 $this是保留变量。

答案 2 :(得分:5)

根据我的评论。 您希望将$this用作传递的变量,并且php不允许它在类方法体外。

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

答案 3 :(得分:0)

你必须先把对象做好。

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