如何解决“在 PHP 5.4 中不应静态调用非静态方法 xxx:xxx()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10768576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I solve "Non-static method xxx:xxx() should not be called statically in PHP 5.4?
提问by kaese
Currently using a large platform in PHP.
目前在PHP中使用大型平台。
The server it's hosted on has recently been upgraded to PHP 5.4.
它所托管的服务器最近已升级到 PHP 5.4。
Since, I've received many error messages like:
从那以后,我收到了许多错误消息,例如:
[Sat May 26 19:04:41 2012] [error] PHP Strict Standards: Non-static method Config::getData() should not be called statically, assuming $this from incompatible context in /xxx/Config.inc.php on line 35
[2012 年 5 月 26 日星期六 19:04:41] [错误] PHP 严格标准:非静态方法 Config::getData() 不应静态调用,假设 $this 来自 /xxx/Config.inc.php 中的不兼容上下文第 35 行
The example method is defined as (note the lack of 'static' keyword):
示例方法定义为(注意缺少 'static' 关键字):
function &getData() {
$configData =& Registry::get('configData', true, null);
if ($configData === null) {
// Load configuration data only once per request, implicitly
// sets config data by ref in the registry.
$configData = Config::reloadData();
}
return $configData;
}
This has no caused a problem before, and I assume the error messages (which cause the application to crash) may be related to the recent upgrade to PHP5.4.
这之前没有造成问题,我认为错误消息(导致应用程序崩溃)可能与最近升级到 PHP5.4 相关。
Is there a PHP setting I can modify to 'ignore' the lack of static keyword?
是否有我可以修改的 PHP 设置以“忽略”缺少 static 关键字?
回答by lanzz
You can either remove E_STRICTfrom error_reporting(), or you can simply make your method static, if you need to call it statically. As far as I know, there is no (strict) way to have a method that can be invoked both as static and non-static method. Also, which is more annoying, you cannot have two methods with the same name, one being static and the other non-static.
您可以删除E_STRICT从error_reporting(),或者你可以简单地让你的方法静态的,如果你需要静态调用它。据我所知,没有(严格的)方法可以作为静态和非静态方法调用的方法。此外,更烦人的是,您不能有两个同名的方法,一个是静态的,另一个是非静态的。
回答by volkinc
Disabling the alert message is not a way to solve the problem. Despite the PHP core is continue to work it makes a dangerous assumptions and actions.
禁用警报消息不是解决问题的方法。尽管 PHP 核心仍在继续工作,但它做出了危险的假设和操作。
Never ignore the error where PHP should make an assumptions of something!!!!
永远不要忽略 PHP 应该对某事做出假设的错误!!!!
If the classorganized as a singleton you can always use function getInstance() and then use getData()
如果将类组织为单例,您可以始终使用函数 getInstance() 然后使用 getData()
Likse:
喜欢:
$classObj = MyClass::getInstance();
$classObj->getData();
If the class is not a singleton, use
如果该类不是单例,请使用
$classObj = new MyClass();
$classObj->getData();
回答by Bruno Campos
I don't suggest you just hidding the stricts errors on your project. Intead, you should turn your method to static or try to creat a new instance of the object:
我不建议您只是隐藏项目中的严格错误。Intead,您应该将您的方法转换为静态或尝试创建对象的新实例:
$var = new YourClass();
$var->method();
You can also use the new way to do the same since PHP 5.4:
自 PHP 5.4 起,您也可以使用新的方式来做同样的事情:
(new YourClass)->method();
I hope it helps you!
我希望它能帮助你!
回答by Daniel Vergara Telechea
I solved this with one code line, as follow: In file index.php, at your template root, after this code line:
我用一行代码解决了这个问题,如下所示:在文件 index.php 中,在您的模板根目录中,在此代码行之后:
defined( '_JEXEC' ) or die( 'Restricted access' );
定义('_JEXEC')或死亡('受限访问');
paste this line: ini_set ('display_errors', 'Off');
粘贴这一行:ini_set('display_errors', 'Off');
Don't worry, be happy...
别着急,开心就好...
posted by Jenio.
杰尼奥发布。

