The Static Context Barrier in PHP
You cannot call a non-static method from a static method, and attempting to do so results in a fatal error regarding an undefined class member. This is not an arbitrary restriction imposed by language designers to confuse developers; it stems from the fundamental difference in how these contexts exist within memory and manage state.
When a static method like Foo::fun2() executes, it operates in a context where no instance of the class exists yet. The self and static keywords resolve to the class itself, not an object instance. Conversely, non-static methods like fun1() require access to $this, which represents a specific instance holding its own properties. Without an instantiated object, there is no $this for the static method to reference.
Attempting to bypass this with syntax tricks like self::fun1() or Foo::fun1() fails because these keywords still refer to the class definition, not an active object. The compiler sees a call to a non-static member on a static type and throws a Fatal Error: Uncaught Error: Call to non-static method Foo::fun1() from context 'static'.
The strict constraint in your scenarioâwhere you cannot alter the visibility of either methodâmeans you must bridge the gap between the two contexts without changing the class definition. You have three viable paths forward, each with distinct trade-offs regarding performance and architectural clarity.
Instantiating an Object Within the Static Method
The most direct solution is to create an instance of the class inside the static method before invoking the non-static function. This satisfies the compiler's requirement for a $this context by explicitly providing one.
class Foo {
public function fun1() {
echo 'non-static';
}
public static function fun2() {
$instance = new self(); // Or new Foo();
$instance->fun1();
}
}
This approach works perfectly but introduces a side effect: it instantiates the class every time fun2 is called. If fun1 performs heavy computation or if you are using static methods for configuration or utility tasks, this repeated instantiation could degrade performance. Furthermore, if fun1 relies on static state or shared resources that behave differently when accessed via an instance versus a static context, this approach might introduce subtle bugs related to state isolation.
Passing the Instance Explicitly
If the architecture of your application allows it, you can refactor the call site to pass the object instance directly into the static method. This preserves the separation of concerns by ensuring that static methods only handle logic that is truly global or configuration-based, while instance methods handle stateful operations.
// Outside the class definition
$foo = new Foo();
Foo::fun2($foo);
// Inside the class
public static function fun2(Foo $instance) {
$instance->fun1();
}
This pattern is often preferable in frameworks like Laravel, where service classes (non-static) are frequently injected into event listeners or closures. However, this requires changes to every single call site throughout your codebase. Since your constraint notes that these functions are used throughout the site and cannot be modified easily, this refactoring path might violate your specific requirements for backward compatibility.
Using a Static Factory or Singleton Pattern
If you must instantiate the class internally but want to avoid creating a new object on every call, consider encapsulating the instantiation logic within a static factory method or utilizing a singleton pattern if the instance is meant to be shared. This keeps the interface of fun2 unchanged while managing the lifecycle of the required instance efficiently.
public static function fun2() {
// Lazy initialization or reuse of a cached instance
static $instance = null;
if ($instance === null) {
$instance = new self();
}
$instance->fun1();
}
This hybrid approach avoids the overhead of repeated instantiation after the first call. However, it couples fun2 to the lifecycle management of fun1, making the static method dependent on instance state. This can be problematic if fun1 is intended to be a pure function that expects no shared mutable state.
Architectural Considerations
The core issue here is a design smell: coupling non-static logic with static entry points. Static methods are typically used for utilities, factories, or configuration that do not depend on object state. Non-static methods usually belong to instances because they manipulate or read state.
If you find yourself frequently needing to call non-static methods from static ones, it is worth re-evaluating whether fun2 should be static at all. Ideally, fun2 should accept an instance as an argument rather than creating one internally, or the logic for calling fun1 should reside entirely within a non-static method that already has access to $this.
If you are building on top of existing legacy code where refactoring is impossible, instantiating the object inside the static method remains the pragmatic choice. It respects the existing signatures while satisfying the language's type safety requirements. Just be mindful of the performance implications and ensure that no unintended side effects arise from creating new objects during high-frequency calls.