PHP - How to solve error "using $this when not in object context"?
The fatal error Using $this when not in object context is a classic symptom of mixing static and instance scopes incorrectly within a trait. It happens because you are attempting to access an instance property or method from a static function, but the $this keyword does not exist in that context. Static methods belong to the class itself, while instance properties belong to specific objects created from that class. They operate in completely different namespaces.
In your specific case, the trait defines a protected property $var and two private static methods. The core issue lies inside Example. When you declare a static method like private static function printSomething(), it runs without an associated object. Therefore, referencing $var directly is impossible because $var is not attached to the class definition; it is attached to any instance of a class that uses this trait.
To fix this immediately, you must align the scope of your methods with how they intend to use data. If printSomething and doSomething need to read or write $var, they should be instance methods, not static ones. You can remove the static keyword from both functions in the trait. This allows them to access $this->var correctly when called on an object:
trait Example
{
protected $var;
private function printSomething() // Removed 'static'
{
print $this->var;
}
private function doSomething() // Removed 'static'
{
// do something with $this->var
}
}
Once the trait methods are updated, your calling class NormalClass can invoke them just like any other instance method. You would call $normalClass->printSomething() inside your logic instead of relying on a static entry point that doesn't have access to the property.
There is a distinct architectural reason why static methods in traits often fail with instance properties: traits are meant for code reuse, but static members in traits do not automatically belong to the class using them. They exist in their own isolated scope. If you truly need a method that runs without an object context (a true static utility), it should not try to access instance properties directly. Instead, pass the necessary data as arguments:
trait Example
{
protected $var;
private static function printSomething($value) // Accept argument instead of property
{
print $value;
}
}
This pattern forces you to explicitly transfer data from the instance to the static method, making dependencies clear and avoiding hidden state leakage. It is a safer approach when designing APIs that might be consumed statically.
When working with Laravel or other frameworks where traits are heavily utilized for features like authentication or event handling, remember that those frameworks almost exclusively use instance methods within traits. Static methods in traits are rare and often indicate a misunderstanding of how scope resolution works. If you find yourself writing static logic inside a trait that needs to talk to the model's data, refactor it into an instance method or move the logic entirely outside the trait into a service class.
The error message is PHP's way of telling you that you are trying to reach into an empty room (the static context) looking for furniture (instance properties) that only appears when someone stands in there (an object). By ensuring that any code accessing $this resides within a non-static method, or by passing data explicitly to static methods, you eliminate this fatal error and create a more predictable class structure.