Home >Backend Development >PHP Tutorial >Why Does Calling a Non-Static Method Statically in PHP Result in an Error?
Understanding Static Method Call Static Method Error
In PHP, the provided code snippet encounters the error message:
Strict standards: Non-static method Page::getInstanceByName() should not be called statically in /var/www/webworks/index.php on line 12
This error occurs when a non-static method in the Page class is called statically, which is not allowed.
Fix
To resolve this issue, the getInstanceByName() method in the Page class needs to be declared as static. Modify the line:
function getInstanceByName($name='')
to:
public static function getInstanceByName($name='')
By declaring the method as static, you can call it using the class name, like:
$r = Page::getInstanceByName($page);
Additional Considerations
The above is the detailed content of Why Does Calling a Non-Static Method Statically in PHP Result in an Error?. For more information, please follow other related articles on the PHP Chinese website!