Coder Perfect

Using $this when not in object context is a fatal error in PHP.

Problem

I’m having a problem:

I’m working on a new WebApp that doesn’t need any frameworks.

I use require once(‘load.php’); in my index.php.

To load my class.php, I use require once(‘class.php’); in load.php.

I’m getting the following issue in my class.php:

An example of how my class.php is written is as follows:

class foobar {

    public $foo;

    public function __construct() {
        global $foo;

        $this->foo = $foo;
    }

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

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

I’m loading maybe foobarfunc() in my index.php like this:

foobar::foobarfunc();

yet it’s also possible

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

What is causing the error?

Asked by ahmet2106

Solution #1

 foobar::foobarfunc();  // Wrong, it is not static method
$foobar = new foobar;  // correct
$foobar->foobarfunc();

Because it is not a static method, you cannot call it this way.

foobar::foobarfunc();

Instead, you should use:

foobar->foobarfunc();

If, on the other hand, you’ve written a static method that looks like this:

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

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

then you can use the following:

foobar::foobarfunc();

Answered by Sarfraz

Solution #2

You’re trying to call a non-static method:

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

The use of a static-call:

foobar::foobarfunc();

The function will be called (even if it is not marked as static) when utilizing a static-call, but there will be no $this because there is no instance of an object.

So :

Because they need to access the $foo property of the class, the methods of your class use the current instance of the class.

This necessitates the use of a class instance in your methods, which implies they can’t be static.

This means you shouldn’t utilize static calls; instead, create the class and use the object to call the methods, like you did in the previous section:

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

Please read the following sections of the PHP manual for more information:

Also, this line in your __construct method is probably unnecessary:

global $foo;

The $foo variable, which is declared outside of all functions and classes, will be visible from within that method if you use the global keyword… And there’s a good chance you don’t have a $foo variable.

You only need to utilize $this->foo to get to the $foo class-property, just like you did before.

Answered by Pascal MARTIN

Solution #3

When you call foobarfunc with the resolution scope operator (::), you’re calling it statically, on the class level rather than the instance level, and you’re utilizing $this when you’re not in object context. In the context of a class, $this does not exist.

If you enable E STRICT, PHP will issue the following warning:

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

Do this instead

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

On a side note, I recommend avoiding the use of global within your classes. Pass something from outside your class through the constructor if you require it inside your class. This is known as Dependency Injection, and it will make your code much easier to maintain and less reliant on external resources.

Answered by Gordon

Solution #4

To begin, remember that $this in a class refers to the current object. That is, to call a class function or variable, you must be formed outside of the class.

As a result, when you execute a class function like foobar::foobarfunc(), no object is produced. However, you wrote return $this->foo inside that method (). Now $this is null and void. That’s why in class.php it says Using $this when not in object context.

Solutions:

Answered by Ramasamy Kasi

Solution #5

$this does not exist when the function is called in a static environment.

Instead, you’d have to use this::xyz().

A useful way to determining what context you’re in when a function can be called statically and in an object instance is detailed in this question: How can I tell if I’m a static or moving object?

Answered by Pekka

Post is based on https://stackoverflow.com/questions/2350937/php-fatal-error-using-this-when-not-in-object-context