Using $this when not in object context
Fatal error: Uncaught Error: Using $this when not in object contextShort 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.
Fix it fast
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.php2Match what you see, then jump to its fix
- 1
The failing function is declared with static.A static method reads $thisFix - 2
The 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
The 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
- 1
What does the error look like?
Fatal error: Uncaught Error: Using $this when not in object contextNon-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.phpConfirmed by php.net
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.phpConfirmed by php.net
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/ | head2Fix
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
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix