Ffile2fix
Sign in Get started

How to fix "Non-static method cannot be called statically" in PHP

Fatal error: Uncaught Error: Non-static method App\Helpers\Formatter::price() cannot be called statically in /var/www/html/views/cart.php:21

The code calls Class::method() but the method is not declared static, so it needs an object instance. PHP 7 only showed a deprecation notice; PHP 8 throws a fatal Error.

Also appears as: Non-static method Foo::bar() cannot be called statically · Deprecated: Non-static method Foo::bar() should not be called statically (PHP 7) · Strict Standards: Non-static method Foo::bar() should not be called statically (PHP 5)

Common causes

  • Legacy code written for PHP 5/7 that called instance methods statically
  • A method that uses $this called as ClassName::method()
  • WordPress hooks registered as 'MyClass::method' or array('MyClass', 'method') for non-static methods
  • Confusing Laravel facades with the underlying class (calling a class directly instead of its facade)
  • A library update that changed a static helper into an instance method

How to fix it

  1. Look at the method definition. Open the class and check whether the method uses $this or object state. That decides whether it should be static.
  2. Call it on an instance. If it uses $this, create or inject an object: $formatter = new Formatter(); $formatter->price($x); or use constructor injection in frameworks.
  3. Make it static if it is stateless. If the method does not touch $this, declare it public static function price(...) and keep the Class::method() calls.
  4. Fix WordPress hook callbacks. Use [$this, 'method'] or [new MyClass(), 'method'] for instance methods; 'MyClass::method' only works for static methods.
  5. Use the facade in Laravel. Static-looking calls like Cache::get() work through facades. Import Illuminate\Support\Facades\Cache, not the underlying Repository class.
  6. Update legacy plugins. If the call is inside an old plugin or theme, update it or find a PHP 8 compatible replacement.

PHP

class Formatter
{
    public static function price(float $v): string   // stateless: static
    {
        return number_format($v, 2);
    }
}

// WordPress, instance method:
add_action('init', [$this, 'register']);

How to stop it happening again

  • Declare stateless helpers as static explicitly
  • Prefer dependency injection over static calls for services
  • Test legacy code on PHP 8 before upgrading

Frequently asked questions

Why did this work before upgrading to PHP 8?

PHP 7 allowed it with a deprecation notice when the method did not use $this. PHP 8 made it a fatal Error in all cases.

Should I just make every method static?

No. Only methods that do not use $this or object properties can be static. Making stateful methods static will break them.

How do Laravel facades call instance methods statically?

A facade's __callStatic() resolves the real object from the container and forwards the call. Calling the underlying class directly with :: does not have that magic.