Maximize your Laravel blog's potential with SEO best practices and reach 10K monthly clicks on Google.
Preview the course for free
Benjamin Crozat New!
Benjamin Crozat The art of crafting web applications

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

Benjamin Crozat — Updated on
Artboard

Hundreds of developers subscribed to my newsletter.
Join them and enjoy free content about the art of crafting websites!

Powered by

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.

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.

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.

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

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.

I show you how to upgrade your Laravel 9 project to version 10 and help you decide whether the return on investment is worth it.

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

Powered by