Fixing PHP 8's 'Non-static method should not be called statically' Error

intermediate๐Ÿ˜ PHP2026-07-02| PHP 7.0+, PHP 8.0+ (Fatal Error), Linux/Windows, Apache/Nginx

Error Message

Fatal error: Uncaught Error: Non-static method User::getName() should not be called statically
#php#oop#static#web-development

The ProblemDuring a recent migration from PHP 7.4 to 8.2, a teammate and I hit a wall with a legacy codebase. While the homepage loaded perfectly, the user dashboard crashed instantly. The error logs revealed the culprit:

Fatal error: Uncaught Error: Non-static method User::getName() should not be called statically

In the PHP 5.6 and 7.x eras, this mistake usually only triggered a Deprecated warning or a Notice. Your script would keep running despite the messy code. However, since the release of PHP 8.0 in November 2020, this has been promoted to a Fatal Error. It is now a hard stop for your application.

Why This HappensThis error occurs when you use the double colon syntax (Class::method()) for a method that wasn't defined with the static keyword. In Object-Oriented Programming, non-static methods belong to a specific instance (an object), while static methods belong to the class definition itself.

Here is a snippet that will break your site in PHP 8:

class User {
    public function getName() {
        return "John Doe";
    }
}

// This triggers the Fatal Error in PHP 8.0+
echo User::getName();

How to Fix ItYou have two main paths to resolve this, depending on whether the method needs to access internal object data.

Method 1: Instantiate the Class (Best for Instance Data)If your method uses $this to access properties, you must create an object first. This is the standard approach for domain models and most application logic.

class User {
    private $name = "John Doe";

    public function getName() {
        return $this->name;
    }
}

// The right way: Create the object, then call the method
$user = new User();
echo $user->getName();

Method 2: Declare the Method as StaticUse this fix if the method is a "pure" helper function that doesn't rely on any internal object state. By adding the static keyword, you tell PHP that the method can be called directly from the class.

class User {
    public static function getName() {
        return "John Doe";
    }
}

// This is now perfectly valid
echo User::getName();

Watch Out for the "$this" TrapBe careful when choosing Method 2. If you add the static keyword to a method that still contains $this, you will immediately trigger a different error: Uncaught Error: Using $this when not in object context. Static methods have no concept of "self" in an instance. If you need those properties, stick to Method 1.

Handling Parent Class CallsLegacy code often triggers this error during inheritance. You can call a non-static parent method using static-like syntax only if you are already inside a non-static child method.

class Base {
    public function log() { /* ... */ }
}

class Child extends Base {
    public function doWork() {
        // This works only because doWork() is also non-static
        parent::log(); 
    }
}

VerificationTest your fix by running the script via the CLI or refreshing your browser. To catch these issues before they reach production, set your error reporting to the highest level in your php.ini file:

error_reporting(E_ALL);
ini_set('display_errors', 1);

If the page renders without the Fatal Error and displays your data correctly, you're good to go.

Key Takeaways- PHP 8 is strict: What used to be a minor warning is now a site-breaking error. Always check logs after an upgrade.- Syntax matters: Use -> for objects and :: for static classes. Mixing them is a major source of technical debt.- Let your IDE help: Tools like PHPStorm or VS Code with the Intelephense extension will highlight these illegal calls in red while you type.

Related Error Notes