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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2350937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:08:29  来源:igfitidea点击:

PHP Fatal error: Using $this when not in object context

phpfunctionclassobjectfatal-error

提问by ahmet2106

I've got a problem:

我有一个问题:

I'm writing a new WebApp without a Framework.

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

In my index.phpI'm using: require_once('load.php');

在我的index.php我使用:require_once('load.php');

And in load.phpI'm using require_once('class.php');to load my class.php.

load.php 中,require_once('class.php');用来加载我的class.php

In my class.phpI've got this error:

在我的class.php我有这个错误:

Fatal error: Using $this when not in object context in class.php on line ... (in this example it would be 11)

致命错误:当不在 class.php 中的对象上下文中时使用 $this 在线...(在本例中为 11)

An example how my class.phpis written:

我的class.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;
    }
}

In my index.phpI'm loading maybe foobarfunc()like this:

在我的index.php我加载可能foobarfunc()是这样的:

foobar::foobarfunc();

but can also be

但也可以

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

Why is the error coming?

为什么会出现错误?

回答by Sarfraz

In my index.php I'm loading maybe foobarfunc() like this:

在我的 index.php 中,我可能像这样加载 foobarfunc():

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

but can also be

但也可以

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

You can not invoke method this way because it is not static method.

您不能以这种方式调用方法,因为它不是静态方法。

foobar::foobarfunc();

You should instead use:

你应该使用:

foobar->foobarfunc();

If however you have created a static method something like:

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

static $foo; // your top variable set as static

public static function foo() {
    return self::$foo;
}

then you can use this:

那么你可以使用这个:

foobar::foobarfunc();

回答by Pascal MARTIN

You are calling a non-static method :

您正在调用非静态方法:

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

Using a static-call :

使用静态调用:

foobar::foobarfunc();

When using a static-call, the function will be called (even if not declared as static), but, as there is no instance of an object, there is no $this.

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

So :

所以 :

  • You should not use static calls for non-static methods
  • Your static methods (or statically-called methods) can't use $this, which normally points to the current instance of the class, as there is no class instance when you're using static-calls.
  • 你不应该对非静态方法使用静态调用
  • 您的静态方法(或静态调用的方法)不能使用 $this,它通常指向类的当前实例,因为在使用静态调用时没有类实例。


Here, the methods of your class are using the current instance of the class, as they need to access the $fooproperty of the class.


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

This means your methods need an instance of the class -- which means they cannot be static.

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

This means you shouldn't use static calls : you should instanciate the class, and use the object to call the methods, like you did in your last portion of code :

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

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


For more informations, don't hesitate to read, in the PHP manual :


有关更多信息,请随时阅读 PHP 手册中的内容:


Also note that you probably don't need this line in your __constructmethod :


另请注意,您的方法中可能不需要此行__construct

global $foo;

Using the globalkeywordwill make the $foovariable, declared outside of all functions and classes, visibile from inside that method... And you probably don't have such a $foovariable.

使用global关键字将使在$foo所有函数和类之外声明的变量从该方法内部可见......而且您可能没有这样的$foo变量。

To access the $fooclass-property, you only need to use $this->foo, like you did.

要访问$fooclass-property,您只需要$this->foo像您一样使用 。

回答by Gordon

If you are invoking foobarfuncwith resolution scope operator(::), then you are calling it statically, e.g. on the class level instead of the instance level, thus you are using $thiswhen not in object context. $thisdoes not exist in class context.

如果您正在调用foobarfunc分辨率范围操作符::),那么你就调用它静态地,如在类级别而不是实例级,因此你$this不用时对象上下文$this在类上下文中不存在。

If you enable E_STRICT, PHP will raise a Notice about this:

如果您启用E_STRICT,PHP 将引发关于此的通知:

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

Do this instead

改为这样做

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

On a sidenote, I suggest not to use globalinside your classes. If you need something from outside inside your class, pass it through the constructor. This is called Dependency Injectionand it will make your code much more maintainable and less dependant on outside things.

在旁注中,我建议不要global在您的课程中使用。如果您需要从类内部外部获取某些内容,请通过构造函数传递它。这称为依赖注入,它将使您的代码更易于维护并且更少依赖于外部事物。

回答by Ramasamy Kasi

First you understand one thing, $thisinside a class denotes the current object.
That is which is you are created out side of the class to call class function or variable.

首先你明白一件事,类中的$this表示当前对象
那就是您在类之外创建以调用类函数或变量。

So when you are calling your class function like foobar::foobarfunc(), object is not created. But inside that function you written return $this->foo(). Now here $this is nothing. Thats why its saying Using $this when not in object context in class.php

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

Solutions:

解决方案:

  1. Create a object and call foobarfunc().

  2. Call foo() using class name inside the foobarfunc().

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

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

回答by A-312

Fast method : (new foobar())->foobarfunc();

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

You need to load your class replace :

你需要加载你的类替换:

foobar::foobarfunc();

by :

经过 :

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

or :

或者 :

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

Or make staticfunction to use foobar::.

或者使静态函数使用foobar::.

class foobar {
    //...

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

回答by Pekka

When you call the function in a static context, $thissimply doesn't exist.

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

You would have to use this::xyz()instead.

你将不得不this::xyz()改用。

To find out what context you're in when a function can be called both statically and in an object instance, a good approach is outlined in this question: How to tell whether I'm static or an object?

为了找出当一个函数既可以静态调用又可以在对象实例中调用时所处的上下文,这个问题概述了一个很好的方法:如何判断我是静态还是对象?

回答by e-satis

$foobar = new foobar;put the classfoobar in $foobar, not the object. To get the object, you need to add parenthesis: $foobar = new foobar();

$foobar = new foobar;foob​​ar 放在 $foobar 中,而不是 object 中。要获取对象,您需要添加括号:$foobar = new foobar();

Your error is simply that you call a method on a class, so there is no $thissince $thisonly exists in objects.

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

回答by Manobendronath Biswas

Just use the Class method using this foobar->foobarfunc();

只需使用 Class 方法即可 foobar->foobarfunc();