代码之家  ›  专栏  ›  技术社区  ›  ahmet2106

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

  •  111
  • ahmet2106  · 技术社区  · 15 年前

    我有个问题:

    我正在写一个没有框架的新webapp。

    在我的 索引文件 我正在使用: require_once('load.php');

    而在 小精灵 我在用 require_once('class.php'); 加载我的 PHP类 .

    在我的 PHP类 我有个错误:

    致命错误:当不在class.php的对象上下文中联机时使用$this…(在这个例子中是11)

    一个例子 PHP类 被写下:

    class foobar {
    
        public $foo;
    
        public function __construct() {
            global $foo;
    
            $this->foo = $foo;
        }
    
        public function foobarfunc() {
            return $this->foo();
        }
    
        public function foo() {
            return $this->foo;
        }
    }
    

    在我的 索引文件 我在装货也许 foobarfunc() 这样地:

    foobar::foobarfunc();
    

    但也可以是

    $foobar = new foobar;
    $foobar->foobarfunc();
    

    为什么会出错?

    8 回复  |  直到 8 年前
        1
  •  147
  •   Sarfraz    15 年前

    在index.php中,我正在加载 foobarfunc()如下:

     foobar::foobarfunc();  // Wrong, it is not static method
    

    但也可以是

    $foobar = new foobar;  // correct
    $foobar->foobarfunc();
    

    不能这样调用方法,因为它不是静态方法。

    foobar::foobarfunc();
    

    您应该改为使用:

    foobar->foobarfunc();
    

    但是,如果您创建了一个静态方法,比如:

    static $foo; // your top variable set as static
    
    public static function foo() {
        return self::$foo;
    }
    

    然后您可以使用:

    foobar::foobarfunc();
    
        2
  •  23
  •   Pascal MARTIN    15 年前

    您正在调用一个非静态方法:

    public function foobarfunc() {
        return $this->foo();
    }
    

    使用静态调用:

    foobar::foobarfunc();
    

    使用静态调用时,将调用函数 (即使没有声明为 static ) ,但是,由于没有对象的实例,因此没有 $this .

    所以:

    • 不应该对非静态方法使用静态调用
    • 静态方法(或静态调用的方法)不能使用$this,它通常指向类的当前实例,因为使用静态调用时没有类实例。


    在这里,类的方法使用类的当前实例,因为它们需要访问 $foo 类的属性。

    这意味着您的方法需要类的实例——这意味着它们不能是静态的。

    这意味着您不应该使用静态调用:您应该实例化类,并使用对象来调用方法,就像您在代码的最后一部分中所做的那样:

    $foobar = new foobar();
    $foobar->foobarfunc();
    


    有关更多信息,请阅读php手册:


    还要注意,您可能不需要在 __construct 方法:

    global $foo;
    

    使用 global keyword 将使 $FO 变量,在所有函数和类外部声明,在该方法内部可见…你可能没有 $FO 变量。

    访问 $FO class-property ,您只需使用 $this->foo 就像你一样。

        3
  •  11
  •   Gordon Haim Evgi    15 年前

    如果您调用 foobarfunc 具有 resolution scope operator ( :: ),那你就叫它 statically ,例如,在类级别而不是实例级别,因此 使用 $this 不在对象上下文中时 . $此 在类上下文中不存在。

    如果启用 E_STRICT ,php将对此发出通知:

    Strict Standards: 
    Non-static method foobar::foobarfunc() should not be called statically
    

    改为这样做

    $fb = new foobar;
    echo $fb->foobarfunc();
    

    另外,我建议不要用 global 在你的课堂里。如果您需要类内部的外部内容,请将其通过构造函数传递。这叫做 Dependency Injection 它将使您的代码更易于维护,并且更少依赖于外部事物。

        4
  •  6
  •   Ramasamy Kasi    11 年前

    首先你明白一件事, $此 类内部表示 当前对象 .
    这就是在类之外创建的用来调用类函数或变量的对象。

    因此,当您像foobar::foobarfunc()那样调用类函数时,不会创建对象。 但在该函数中,您编写的返回$this->foo()。现在这里$这没什么。这就是为什么它说 在class.php的对象上下文中不使用$this

    解决:

    1. 创建一个对象并调用foobarfunc()。

    2. 在foobarfunc()中使用类名调用foo()。

        5
  •  4
  •   Community CDub    8 年前

    在静态上下文中调用函数时, $this 根本不存在。

    你必须使用 this::xyz() 相反。

    为了找出在什么上下文中可以静态调用函数和在对象实例中调用函数,在这个问题中概述了一种好的方法: How to tell whether I’m static or an object?

        6
  •  4
  •   user2226755    9 年前

    快速方法: (new foobar())->foobarfunc();

    您需要加载类替换:

    foobar::foobarfunc();
    

    通过:

    (new foobar())->foobarfunc();
    

    或:

    $Foobar = new foobar();
    $Foobar->foobarfunc();
    

    或使 静止的 要使用的函数 foobar:: .

    class foobar {
        //...
    
        static function foobarfunc() {
            return $this->foo();
        }
    }
    
        7
  •  0
  •   Bite code    15 年前

    $foobar = new foobar; 班级 Foobar在$Foobar, 不是目标 . 要获取对象,需要添加括号: $foobar = new foobar();

    你的错误仅仅是你调用了一个类的方法,所以没有 $this 自从 $此 只存在于对象中。

        8
  •  -1
  •   Manobendronath Biswas    9 年前

    使用这个类方法 foobar->foobarfunc();

    推荐文章