Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Using $this when not in object context

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

Short answer

$this only exists while a method runs on an object. The error means the code reached $this without an object behind it. The usual reasons are a static method or static closure that reads $this, a plain function outside a class, or a non-static method called with the class name. Make the method an instance method or call it on an object.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the method named in the trace. Look at its declaration (is it static?) and at how it was called (Class::method() or $object->method()).

    grep -n "static function\|::" src/YourClass.php
  2. 2Match what you see, then jump to its fix

    1. 1The failing function is declared with static.A static method reads $thisFix
    2. 2The call looks like Foo::bar() while bar() is a normal method. PHP 8 reports the Non-static method error instead; PHP 7 left $this undefined.A non-static method is called with the class nameFix
    3. 3The failing code is a function() { ... $this ... } with the static keyword, or a function that is not inside a class.A closure was declared static, or $this is used in a plain functionFix

What does the error look like?

  • Fatal error: Uncaught Error: Using $this when not in object context
  • Non-static method A::foo() cannot be called statically

Cause 1 of 3

A static method reads $this

1Check

Static methods run without an instance, so $this is not available inside them.

grep -n -A6 "static function" src/YourClass.php | grep -n 'this'

2Fix

Remove static if the method needs the object, or keep it static and pass in what it needs (or use self:: / static:: for other static members).

class Foo {
    public $value = 1;
    public function read() { return $this->value; }      // instance method
    public static function make(): static { return new static(); }
}
echo (new Foo)->read();

3Verify

Call it on an object.

php foo.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

A non-static method is called with the class name

1Check

Calling a non-static method statically throws an Error in PHP 8. Before PHP 8.0 it was a deprecation notice and $this stayed undefined.

grep -rn "Foo::bar(" src/

2Fix

Create the object and call the method on it, or inject the object where it is needed.

$foo = new Foo();
$foo->bar();

3Verify

Run the code path again.

php index.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

A closure was declared static, or $this is used in a plain function

1Check

A closure declared in a method gets the object bound automatically, but a static closure does not. A function outside any class never has an object.

grep -rn "static function\|static fn" src/ | head

2Fix

Drop static from the closure, or pass the object with use ($obj).

class Foo {
    public $name = 'x';
    public function run() {
        $f = function () { return $this->name; };   // not static
        return $f();
    }
}

3Verify

Run the method.

php -r 'require "foo.php"; echo (new Foo)->run();'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Make a method static only when it truly needs no object state.
  • Add static analysis (PHPStan) so $this inside static code is caught in CI.
  • Do not call instance methods with ::; inject the object instead.
  • Read the first stack-trace frame; it names the exact call that lacked an object.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.