“I created an AI assistant for Laravel developers that handles all the boring work.”
Learn more
Smousss
Benjamin Crozat The art of crafting web applications

Here's the fix to "using $this when not in object context."

Benjamin Crozat — Updated on
Here's the fix to "using $this when not in object context."

To fix “Using $this when not in object context”, you can make the static method that is calling $this non-static.

No matter if you’re using CodeIgniter, CakePHP, Laravel, Symfony, WordPress, Yii, or anything else, $this is a variable that refers to the current object. Therefore, it’s natural to not being allowed to call it from a static method.

How to fix “Using $this when not in object context”, by example

Take this code and try to run it. You will see “Using $this when not in object context” again.

class Foo {
public static function bar() {
// This is bad because we are in a static method.
$this->baz();
}
 
public function baz() {
}
}
 
Foo::bar();

As you can see, we are trying to call baz(), which is a non-static method, from a static method.

As mentionned above, we need to:

  1. Remove the static keyword from bar()’s declaration;
  2. Create an instance of Foo and call bar() from there.
class Foo {
- public static function bar() {
+ public function bar() {
$this->baz();
}
 
public function baz() {
}
}
 
-Foo::bar();
+$foo = new Foo;
+$foo->bar();

You could also make the baz method static depending on your initial intention:

class Foo {
public static function bar() {
- $this->baz();
+ static::baz();
}
 
- public function baz() {
+ public static function baz() {
}
}
Recommended

Debugging requires dissecting everything. Here's a list of all the one-line of code built-in ways to print arrays in PHP (and even Laravel-specific helpers).

There are multiple ways to check if an array is empty. Let me tell you about each of them and why and when you should use them.

Laravel Collections make arrays more powerful and convenient to work with. This article provides tons of quick tips to instantly make your codebase better.

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

PHP 8.3 will be released in November 2023, and as usual, you need to be up to date with new features and breaking changes for easier transitions.

Laravel 10 has been released on February 14, 2023. Let's dive into every relevant new feature and change.

Learn why and how to fix "Methods with the same name as their class will not be constructors in a future version of PHP" warnings.

Here's a case study for my blog in the programming niche, where I share everything I did to increase clicks by a huge amount since the beginning.

Learn why the "Invalid argument supplied for foreach()" warning happens, and let me show you multiple ways to fix it.

Powered by