Home > Backend Development > PHP Tutorial > How to Fix the \'Strict Standards: Non-static method should not be called statically\' PHP Error?

How to Fix the \'Strict Standards: Non-static method should not be called statically\' PHP Error?

DDD
Release: 2024-11-27 05:32:10
Original
921 people have browsed it

How to Fix the

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

The error message "Strict standards: Non-static method should not be called statically" occurs when a non-static method is accessed using a static invocation. To address this error, we will modify the code to ensure that all methods are declared as static when intended to be called statically.

In the provided code, the methods in the Page class are not declared as static, but they are being called as static. To resolve this issue, we need to add the static keyword to the method declarations.

Modify Page.php

class Page{
    // ...
    public static function getInstanceByName($name=''){
        // ...
    }
    // ...
}
Copy after login

By making these methods static, they can be accessed using the class name, as seen in the index.php file:

// ...
if($page){ // load by name
    $r = Page::getInstanceByName($page);
    if($r && isset($r->id)) $id = $r->id;
}
// ...
Copy after login

Additional Notes

  • Static methods and Singleton patterns can hinder unit testing.
  • Constructors should primarily focus on setting the object to a valid state rather than performing extensive queries.
  • Constructors cannot return values; they always return void.

The above is the detailed content of How to Fix the \'Strict Standards: Non-static method should not be called statically\' PHP Error?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template