Error message Strict standards: Non-static method should not be called statically in php

Error message Strict standards: Non-static method should not be called statically in php

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

Stefan Bogdanescu

Founder & Senior Architect

Strict standards: Non-static method should not be called statically in PHP

You are encountering a classic PHP error that stems from a mismatch between how you are invoking a method and how that method is defined. The specific message, Strict standards: Non-static method Page::getInstanceByName() should not be called statically, indicates that your code in index.php attempts to use the :: operator on Page::getInstanceByName(), while the definition of that method inside the Page class lacks the static keyword.

In the PHP source provided for ww.incs/basics.php, a custom autoloader is defined using the deprecated __autoload magic constant. This means the file Page.php is loaded dynamically based on its filename, which is fine for legacy code but worth noting when debugging class resolution issues. The error occurs at line 12 of index.php, specifically where $r=Page::getInstanceByName($page); is executed.

To resolve this without altering your application architecture significantly, you have two primary options depending on whether the method actually needs to be static or if it simply was declared that way by mistake.

The Static Keyword Requirement

PHP enforces strict rules regarding class methods. If a method requires access to instance-specific data (like $this), it must be defined as an instance method. Conversely, if a method can function entirely within the context of the class and does not need to interact with a specific object instance, it should be marked static.

Looking at your Page class definition in ww.incs/basics.php, the method is currently defined as:

function getInstanceByName($name='') {
    // ...
}

Because there is no static keyword preceding function, PHP treats this as an instance method. However, the call in index.php uses the static syntax Page::getInstanceByName(). This triggers the "Non-static method should not be called statically" warning.

Option 1: Convert to a Static Method (Recommended)

Given that getInstanceByName utilizes singleton-like logic—checking $instancesByName arrays and returning an existing instance or creating a new one—it is logically designed to behave as a static factory method. It does not depend on $this for its core logic; it relies entirely on the class's static properties.

The fix is straightforward: add the static keyword to the method declaration. This aligns the definition with your usage in index.php.

Update the Page class as follows:

function static getInstanceByName($name='') { // <-- Add 'static' here
    $name = strtolower($name);
    $nameIndex = preg_replace('#[^a-z0-9/]#', '-', $name);
    
    if (@array_key_exists($nameIndex, self::$instancesByName)) {
        return self::$instancesByName[$nameIndex];
    }
    
    // Note: Ensure you use 'self' or 'static' for the property access inside new Page() calls
    // depending on how you intend to handle inheritance later.
    self::$instancesByName[$nameIndex] = new Page($name, 1);
    return self::$instancesByName[$nameIndex];
}

You must also check the other two factory methods in your class. getInstanceBySpecial has a similar bug where it uses $instancesBySpecial instead of self::$instancesBySpecial. This is a scope resolution error that will cause undefined variable notices once you fix the static call issue:

function getInstanceBySpecial($sp=0) {
    if (!is_numeric($sp)) return false;
    
    // Fix: Use 'self' to access the static property correctly
    if (!@array_key_exists($sp, self::$instancesBySpecial)) { 
        self::$instancesBySpecial[$sp] = new Page($sp, 3);
    }
    return self::$instancesBySpecial[$sp];
}

Option 2: Change the Call to Instance Context

If you prefer not to modify the class definition immediately, you can change how you call it in index.php. However, this approach is generally inferior because it requires creating a temporary object just to access a static utility, which defeats the purpose of the factory pattern you are implementing.

You could write:

$page = new Page(); // This might fail if __construct logic doesn't handle empty inputs gracefully
$result = $page->getInstanceByName($page);

This is messy and likely to break your singleton caching logic, which relies on accessing the static $instancesByName array directly.

Why This Matters for Stability

The Strict standards warning is more than just noise; it indicates a violation of the language's core contract regarding class scope. Leaving this unresolved can lead to unpredictable behavior, especially if you later introduce child classes that override these methods. By explicitly marking the method as static, you ensure that PHP knows exactly which context to use, eliminating ambiguity and preventing warnings from escalating into errors in future PHP versions.

If you are maintaining a legacy codebase like this, remember that while __autoload is deprecated in modern PHP versions (replaced by spl_autoload_register), fixing the static context issues is a low-risk, high-reward refactoring step that improves code clarity and stability. For modern projects, consider migrating to Composer autoloading to handle these class definitions more robustly.

Our network