Non-static method ..... should not be called statically

Non-static method ..... should not be called statically

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

Stefan Bogdanescu

Founder & Senior Architect

The Static vs. Non-Static Method Trap in PHP 5.4

When updating a legacy codebase to PHP 5.4, you might encounter an error that stops execution immediately: PHP Strict Standards: Non-static method VTimer::get() should not be called statically. This specific issue usually manifests when trying to instantiate a singleton-like object using a class method that wasn't designed for static invocation in that version of the language.

In the scenario provided, the error points to line 371 in function_smarty.php with the following code:

$timer = VTimer::get($options['magic']);

The core problem here is a mismatch between how the method is defined and how it is being invoked. If VTimer::get() is intended to be a factory method or a singleton getter, but it is not declared as static in its signature, PHP 5.4 enforces a strict boundary between class-level operations and instance-level operations.

Understanding the Error Signature

In older versions of PHP (prior to 5.3), developers often relied on loose typing and magic methods to handle such discrepancies. You could theoretically call a non-static method statically, and PHP would implicitly instantiate the class first. However, PHP 5.4 introduced Strict Standards warnings that became fatal errors in many production environments if not handled.

The error message explicitly states that get() is a non-static method. This means the underlying definition looks something like this:

class VTimer {
    public function get($magic) {
        // Logic to create or retrieve a timer instance
        return new self();
    }
}

When you call VTimer::get(), PHP expects get() to be a static method, meaning it doesn't require an object instance to run. Since the definition lacks the static keyword inside the parentheses, the interpreter refuses to bridge the gap automatically.

The Correct Fix: Adding the Static Keyword

The most direct resolution is to modify the method definition within the VTimer class. You need to add the static modifier to the method signature. This changes the implementation to:

class VTimer {
    public static function get($magic) {
        // Logic to create or retrieve a timer instance
        return new self();
    }
}

By doing this, you are explicitly telling PHP that this method belongs to the class itself rather than an instance of the class. Now, calling VTimer::get($options['magic']) is valid and follows the principle of least surprise for any developer reading the code later.

Alternative Approaches and Trade-offs

If modifying the source file of VTimer is not immediately possible due to external dependencies or a read-only filesystem, you have two main alternatives:

  1. Change the Call Site: Modify the calling code to instantiate the class first before accessing the method.

    $instance = new VTimer();
    $timer = $instance->get($options['magic']);
    

    However, this is often undesirable if the original design intended VTimer::get() to act as a global factory or singleton accessor. Instantiating VTimer unnecessarily just to call get() defeats the purpose of the static interface.

  2. Use Magic Method Fallbacks: In some legacy scenarios, developers try to wrap the call in a helper that attempts to instantiate the class if the static call fails. While this works around the error, it masks the underlying architectural inconsistency and makes debugging harder. It is generally better to fix the root cause by updating the method signature.

Why PHP 5.4 Matters Here

This specific error highlights a significant shift in PHP's evolution. The transition to strict standards was intended to prevent subtle bugs caused by implicit instantiation. Before PHP 5.3, calling a non-static method statically would trigger a warning but often continue execution if the class had a __call or __get magic method to handle it. In PHP 5.4, that leniency was removed for methods explicitly defined as public without the static modifier.

If you are maintaining code from this era, always verify that any class-level operations (like accessing constants, magic methods, or factory functions) are declared with the static keyword. This ensures compatibility not just with PHP 5.4, but also with modern PHP versions where strict mode is the default expectation.

For projects built on top of these legacy patterns, understanding these distinctions helps avoid runtime crashes during server upgrades. If you are porting such applications to a modern framework like Laravel, you will find that these static binding rules are even more rigid, so resolving this early in the migration process saves significant debugging time later.

Our network