Get your next remote job on LaraJobs.
1 contributor Edit on GitHub PHP

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

Table of contents:

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

Introduction

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 function bar() {
$this->baz();
}
 
public function baz() {
}
}
 
$foo = new Foo;
$foo->bar();

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

class Foo {
public static function bar() {
static::baz();
}
 
public static function baz() {
}
}
Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

PHP 8.3 is out, now! Here's what's new and changed. PHP 8.3 is out, now! Here's what's new and changed.

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

Modified on Nov 23, 2023

PHP
Print an array with PHP (+ Laravel) Print an array with PHP (+ Laravel)

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).

Modified on Jun 24, 2023

14 Laravel Collections tips to refactor your codebase 14 Laravel Collections tips to refactor your codebase

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

Modified on Aug 27, 2023

console.log() in PHP console.log() in PHP

Explore the world of PHP debugging with var_dump(), and Laravel's friendlier alternatives, dump() and dd(). Much charm, such useful!

Modified on Sep 6, 2023

PHP

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here

Partners

If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -