PHP 致命错误:不在对象上下文中时使用 $this
我遇到了一个问题:
我正在编写一个没有框架的新 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您不能以这种方式调用该方法,因为它不是静态方法。
您应该使用:
但是,如果您创建了一个类似于以下内容的静态方法:
那么您可以使用:
You can not invoke the method this way because it is not a static method.
You should instead use:
If however, you have created a static method something like:
then you can use 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.
这意味着您的方法需要类的实例——这意味着它们不能是静态的。
这意味着您不应该使用静态调用:您应该实例化该类,并使用该对象来调用方法,就像您在代码的最后部分中所做的那样:
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
变量在该方法内部可见......并且您可能没有这样的$foo
变量。访问
$foo
class-property< /a>,你只需要像你一样使用$this->foo
。You are calling a non-static method :
Using a static-call :
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 :
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 :
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 :
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.如果您使用 解析范围调用
foobarfunc
运算符 (::
),那么你就调用它 静态,例如在类级别而不是实例级别,因此您在不在对象上下文中时使用$this
。$this
在类上下文中不存在。如果您启用
E_STRICT
,PHP 将提出有关此问题的通知:请执行此
操作 旁注,我建议不要在类中使用
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:Do this instead
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.首先你要明白一件事,类中的$this表示当前对象。
也就是说,您是在类外部创建的以调用类函数或变量。
因此,当您调用类函数(如 foobar::foobarfunc())时,不会创建对象。
但在该函数中,您编写了 return $this->foo()。现在这里$this什么都不是。这就是为什么它说 在 class.php 中不在对象上下文中时使用 $this
解决方案:
创建一个对象并调用 foobarfunc()。
使用 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:
Create a object and call foobarfunc().
Call foo() using class name inside the foobarfunc().
快速方法:(new foobar())->foobarfunc();
您需要加载类,将 : 替换
为 :
或 :
或制作静态函数才能使用
foobar::
。Fast method : (new foobar())->foobarfunc();
You need to load your class replace :
by :
or :
Or make static function to use
foobar::
.当您在静态上下文中调用该函数时,
$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?
在我看来,这是 PHP 中的一个错误。
错误
出现在使用
$this
的函数中,但错误是调用函数将非静态函数用作静态函数。即:虽然错误在这里:
It seems to me to be a bug in PHP.
The error
appears in the function using
$this
, but the error is that the calling function is using non-static function as a static. I.e:While the error is here:
$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.只需使用
foobar->foobarfunc();
即可使用 Class 方法Just use the Class method using this
foobar->foobarfunc();