PHP Fatal error: Using $this when not in object context

PHP Fatal error: Using $this when not in object context

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Fatal error: Using $this when not in object context

When you encounter the Fatal error: Using $this when not in object context, it is almost always a sign that your code is attempting to instantiate an object without explicitly invoking the constructor, or worse, calling a static method on a class where instance data is required. This specific issue often trips up developers building custom web applications from scratch who are transitioning from procedural scripts to object-oriented patterns.

In the scenario you described, the error occurs in class.php at line 11, inside the __construct() method. The culprit is the assignment $this->foo = $foo;. Here, $this refers to the current instance of the foobar class. However, $this only exists within the context of an object being created or already existing. If you attempt to access $this outside of an object instantiation—such as when calling foobar::foobarfunc() directly without using new—the engine cannot resolve what object you are referring to, resulting in the fatal error.

The Mechanics of Static vs. Instantiation

The root cause lies in how PHP resolves class methods and properties. In your index.php, you have two ways of interacting with the foobar class:

  1. Instantiation: $foobar = new foobar;
  2. Static Call: foobar::foobarfunc();

When you use new foobar, PHP allocates memory for a new object, initializes its properties (setting them to null by default), and then executes the __construct() method. At this exact moment, $this is bound to that specific memory block, allowing the constructor to read the global variable $foo and assign it to the instance property $foo.

However, when you call foobar::foobarfunc(), PHP treats foobarfunc as a static method. Static methods are executed in the scope of the class itself, not an instance. At line 11, inside __construct, if the constructor logic assumes it is always running within an object context but the method is somehow called statically (or if the flow jumps directly to foobarfunc without instantiation), the binding fails.

More critically, if you call foobar::foobarfunc() directly, PHP looks for a static definition of foobarfunc. If foobarfunc is not declared as public static, PHP will try to find an instance to call it on. But because index.php likely hasn't instantiated the class before calling the static method, or if the static method internally tries to access $this, you hit this wall immediately.

Fixing the Architecture

To resolve this in your existing structure, you must ensure that any method relying on instance properties (like $this->foo) is only called after an object has been instantiated. If you intend to use foobar::foobarfunc() in your main script, you have two architectural options:

Option A: Make the Method Static
If foobarfunc does not need to access other instance-specific data beyond what is passed as arguments or global variables, make it static. This removes the dependency on $this.

class foobar {
    public $foo;

    // Keep constructor for initialization if needed later
    public function __construct() {
        global $foo;
        $this->foo = $foo;
    }

    // Change to static if it doesn't need instance state
    public static function foobarfunc() {
        // You can still use global variables here if preferred
        // but you cannot access $this anymore
        return 'Static call result'; 
    }

    // Or, keep it non-static and require instantiation in index.php:
    /*
    public function foobarfunc() {
        return $this->foo();
    }
    */
}

Option B: Enforce Instantiation
If foobarfunc relies on the internal state set by __construct, you must change your usage in index.php. The static call syntax (::) bypasses the object creation step. You are forced to use the instantiation syntax (new).

// In index.php
$foobar = new foobar(); // Constructor runs, $this is valid
$result = $foobar->foobarfunc();

Trade-offs in Custom Frameworks

When building a web app without a framework, you have full control over these patterns, but you also bear the responsibility of enforcing them. A common pitfall is mixing static and instance methods carelessly. Static methods are useful for utility functions that don't depend on class state, while instance methods are required when maintaining state (like $foo).

If you find yourself frequently needing to pass data into your class via the constructor, consider injecting dependencies or using a dedicated initialization method rather than relying heavily on global variables inside __construct. While using globals like global $foo works for quick prototypes, it makes testing difficult and couples your class tightly to the execution environment.

If you prefer a more structured approach similar to how frameworks handle this, look into resource-heavy solutions that manage object lifecycles automatically. For instance, Laravel provides robust mechanisms for dependency injection via its service container, ensuring that objects are always instantiated correctly before methods are called. You can explore these patterns at https://laravelcompany.com if you ever decide to migrate from a bare-bones setup to a more structured framework later on.

The error is not a bug in PHP; it is a safeguard preventing undefined behavior when instance data is accessed without an actual instance existing. Aligning your method signatures with their intended usage—static for stateless logic, instance for stateful logic—is the key to stability.

Our network