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

发布于 2024-08-22 21:22:57 字数 945 浏览 8 评论 0原文

我遇到了一个问题:

我正在编写一个没有框架的新 Web 应用程序。

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

load.php 中,我使用 < code>require_once('class.php'); 加载我的 class.php

在我的 class.php 中,我遇到了以下错误:

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

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

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

foobar::foobarfunc();

但也可以是

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

为什么会出现错误?

I've got a problem:

I'm writing a new WebApp without a Framework.

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

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

In my class.php I've got this error:

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

An example how my class.php is written:

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.php I'm loading maybe foobarfunc() like this:

foobar::foobarfunc();

but can also be

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

Why is the error coming?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

吖咩 2024-08-29 21:22:57

在我的index.php中我可能正在加载
foob​​arfunc() 像这样:

 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 foobarfunc() {
    return self::$foo;
}

那么您可以使用:

foobar::foobarfunc();

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

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

but can also be

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

You can not invoke the method this way because it is not a 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 foobarfunc() {
    return self::$foo;
}

then you can use this:

foobar::foobarfunc();
唔猫 2024-08-29 21:22:57

您正在调用非静态方法:

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

使用静态调用:

foobar::foobarfunc();

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

所以:

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

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

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

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

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

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

Also note that you probably don't need this line in your `__construct` method :

global $foo;

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

访问 $foo class-property< /a>,你只需要像你一样使用 $this->foo

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.

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.

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

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 :

Also note that you probably don't need this line in your `__construct` method :

global $foo;

Using the global keyword will make the $foo variable, declared outside of all functions and classes, visibile from inside that method... And you probably don't have such a $foo variable.

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

入画浅相思 2024-08-29 21:22:57

如果您使用 解析范围调用 foobarfunc运算符 (::),那么你就调用它 静态,例如在类级别而不是实例级别,因此您在不在对象上下文中时使用$this$this 在类上下文中不存在。

如果您启用 E_STRICT,PHP 将提出有关此问题的通知:

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

请执行此

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

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

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

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

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 global inside your classes. If you need something from outside inside your class, pass it through the constructor. This is called Dependency Injection and it will make your code much more maintainable and less dependant on outside things.

背叛残局 2024-08-29 21:22:57

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

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

解决方案:

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

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

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

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

Solutions:

  1. Create a object and call foobarfunc().

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

快乐很简单 2024-08-29 21:22:57

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

您需要加载类,将 : 替换

foobar::foobarfunc();

为 :

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

或 :

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

或制作静态函数才能使用foobar::

class foobar {
    //...

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

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

You need to load your class replace :

foobar::foobarfunc();

by :

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

or :

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

Or make static function to use foobar::.

class foobar {
    //...

    static function foobarfunc() {
        return $this->foo();
    }
}
心不设防 2024-08-29 21:22:57

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

您必须使用 this::xyz() 来代替。

要了解当可以静态调用函数和在对象实例中调用函数时您所处的上下文,此问题中概述了一个好方法:如何判断我是静态的还是对象?

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

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

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?

帅气尐潴 2024-08-29 21:22:57

在我看来,这是 PHP 中的一个错误。
错误

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

出现在使用 $this 的函数中,但错误是调用函数将非静态函数用作静态函数。即:

Class_Name
{
    function foo()
    {
        $this->do_something(); // The error appears there.
    }
    function do_something()
    {
        ///
    }
}

虽然错误在这里:

Class_Name::foo();

It seems to me to be a bug in PHP.
The error

'Fatal error: Uncaught Error: Using $this when not in object context
in'

appears in the function using $this, but the error is that the calling function is using non-static function as a static. I.e:

Class_Name
{
    function foo()
    {
        $this->do_something(); // The error appears there.
    }
    function do_something()
    {
        ///
    }
}

While the error is here:

Class_Name::foo();
记忆之渊 2024-08-29 21:22:57

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

您的错误很简单,您调用了类上的方法,因此没有 $this 因为 $this 只存在于对象中。

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

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

会傲 2024-08-29 21:22:57

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文