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

时间:2022-10-15 19:56:10

I've got a problem:

我有一个问题:

I'm writing a new WebApp without a Framework.

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

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

在我的索引。我使用php:require_once(load.php);

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

和负载。我使用php require_once(class.php);我class.php加载。

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

在我的类。php我有这个错误:

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

致命错误:在类中非对象上下文中使用$this。php在线…(本例中为11)

An example how my class.php is written:

这是我的课的一个例子。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.php I'm loading maybe foobarfunc() like this:

在我的索引。我加载的是foobarfunc()

foobar::foobarfunc();

but can also be

但也可以

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

Why is the error coming?

为什么会出现错误?

9 个解决方案

#1


122  

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

在我的索引。我加载的是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();

#2


19  

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.

当使用静态调用时,函数将被调用(即使没有声明为静态),但是由于没有对象实例,所以没有$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 $foo property 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 __construct method :

还要注意,在__construct方法中您可能不需要这一行:

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.

使用global关键字将使$foo变量,在所有函数和类之外声明,visibile从该方法内部…你可能没有$foo变量。

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

要访问$foo类属性,您只需使用$this->foo,就像您所做的那样。

#3


10  

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.

如果您正在使用resolution scope operator(::)调用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 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.

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

#4


6  

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.

首先,您了解一件事,在类中$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()时,对象不会被创建。但是在你写的函数里面返回$this->foo()这里的$这没什么。这就是为什么它说在class.php的对象上下文中不使用$this

Solutions:

解决方案:

  1. Create a object and call foobarfunc().

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

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

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

#5


4  

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

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

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

您必须使用以下命令: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?

为了找出在静态和对象实例中都可以调用函数时所处的环境,在这个问题中概述了一种很好的方法:如何判断我是静态的还是对象?

#6


4  

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::.

或制作静态功能使用foobar:::。

class foobar {
    //...

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

#7


0  

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

foobar = new foobar美元;将类foobar放在$foobar中,而不是对象中。要获取对象,需要添加括号:$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.

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

#8


0  

so simple : ..!

如此简单:. . !

just insert static before your property and your function;

在属性和函数之前插入静态;

#9


-1  

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

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

#1


122  

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

在我的索引。我加载的是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();

#2


19  

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.

当使用静态调用时,函数将被调用(即使没有声明为静态),但是由于没有对象实例,所以没有$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 $foo property 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 __construct method :

还要注意,在__construct方法中您可能不需要这一行:

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.

使用global关键字将使$foo变量,在所有函数和类之外声明,visibile从该方法内部…你可能没有$foo变量。

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

要访问$foo类属性,您只需使用$this->foo,就像您所做的那样。

#3


10  

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.

如果您正在使用resolution scope operator(::)调用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 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.

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

#4


6  

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.

首先,您了解一件事,在类中$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()时,对象不会被创建。但是在你写的函数里面返回$this->foo()这里的$这没什么。这就是为什么它说在class.php的对象上下文中不使用$this

Solutions:

解决方案:

  1. Create a object and call foobarfunc().

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

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

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

#5


4  

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

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

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

您必须使用以下命令: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?

为了找出在静态和对象实例中都可以调用函数时所处的环境,在这个问题中概述了一种很好的方法:如何判断我是静态的还是对象?

#6


4  

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::.

或制作静态功能使用foobar:::。

class foobar {
    //...

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

#7


0  

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

foobar = new foobar美元;将类foobar放在$foobar中,而不是对象中。要获取对象,需要添加括号:$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.

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

#8


0  

so simple : ..!

如此简单:. . !

just insert static before your property and your function;

在属性和函数之前插入静态;

#9


-1  

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

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