php 严格标准:“ ”的声明应与“ ”兼容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17234259/
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
Strict Standards: Declaration of ' ' should be compatible with ' '
提问by egig
I just installed woocommerce 2.0 (on Wordpress) on PHP 5.4, and I got this:
我刚刚在 PHP 5.4 上安装了 woocommerce 2.0(在 Wordpress 上),我得到了这个:
Strict Standards: Declaration of WC_Gateway_BACS::process_payment() should be compatible with WC_Payment_Gateway::process_payment() in D:\my\path\to\htdocs\wordpress\plugins\woocommerce\classes\gateways\bacs\class-wc-gateway-bacs.php on line...
严格标准:WC_Gateway_BACS::process_payment() 的声明应与 D:\my\path\to\htdocs\wordpress\plugins\woocommerce\classes\gateways\bacs\class-wc-gateway 中的 WC_Payment_Gateway::process_payment() 兼容-bacs.php 上线...
I check the files and found that WC_Payment_Gateway
have no method process_payment()
.
I need to know how to resolve this (not by setting error_reporting()
).
我检查了文件,发现WC_Payment_Gateway
没有任何方法process_payment()
。我需要知道如何解决这个问题(不是通过设置error_reporting()
)。
What is Strict Standardsin PHP exactly?
In what condition so we get that error?
PHP 中的严格标准到底是什么?
在什么情况下我们会得到那个错误?
采纳答案by VolkerK
WC_Payment_Gateway
is defined in abstract-wc-payment-gateway.php
and declares a method
WC_Payment_Gateway
定义abstract-wc-payment-gateway.php
并声明了一个方法
function process_payment() {}
while WC_Gateway_BACS
defines it as
而WC_Gateway_BACS
将其定义为
function process_payment( $order_id ) { ...
(maybe you mixed up WC_Payment_Gateway and WC_Payment_Gateways).
(也许你混淆了 WC_Payment_Gateway 和 WC_Payment_Gateway s)。
So, different signature (0 parameters vs 1 parameter) -> strict error.
Since it seems* to be used always with one parameter you could change
因此,不同的签名(0 个参数与 1 个参数)-> 严格错误。
由于它似乎总是与一个参数一起使用,因此您可以更改
function process_payment() {}
to
到
function process_payment($order_id) {}
(*) keep in mind I know of woocommerce only since the last five minutes, so don't take my word for it.
(*) 请记住,我是在过去五分钟才知道 woocommerce 的,所以不要相信我的话。
回答by paranoid
Quote from PHP Manual
引用自 PHP 手册
In PHP 5 a new error level E_STRICT is available. Prior to PHP 5.4.0 E_STRICT was not >included within E_ALL, so you would have to explicitly enable this kind of error level in >PHP < 5.4.0. Enabling E_STRICT during development has some benefits. STRICT messages >provide suggestions that can help ensure the best interoperability and forward >compatibility of your code. These messages may include things such as calling non-static >methods statically, defining properties in a compatible class definition while defined in >a used trait, and prior to PHP 5.3 some deprecated features would issue E_STRICT errors >such as assigning objects by reference upon instantiation.
在 PHP 5 中,新的错误级别 E_STRICT 可用。在 PHP 5.4.0 之前,E_STRICT 未包含在 E_ALL 中,因此您必须在 >PHP < 5.4.0 中显式启用这种错误级别。在开发期间启用 E_STRICT 有一些好处。STRICT 消息 > 提供有助于确保最佳互操作性和转发 > 代码兼容性的建议。这些消息可能包括诸如静态调用非静态 >methods、在定义在 >a used trait 时在兼容类定义中定义属性,以及在 PHP 5.3 之前一些不推荐使用的功能会发出 E_STRICT 错误 >例如通过引用分配对象实例化。
You are receiving this error because WC_Gateway_BACS::process_payment()declaration is different than WC_Payment_Gateway::process_payment()(might be not the same amount of parameters etc). If WC_Payment_Gatewayhas no method process_payment, check it's parent class :)
您收到此错误是因为WC_Gateway_BACS::process_payment()声明与WC_Payment_Gateway::process_payment() 不同(可能不是相同数量的参数等)。如果WC_Payment_Gateway没有方法 process_payment,请检查它的父类 :)
Also, if you want to disable STRICT errors, add ^ E_STRICTto your error reporting configuration, for example:
此外,如果您想禁用 STRICT 错误,请将^ E_STRICT添加到您的错误报告配置中,例如:
error_reporting(E_ALL ^ E_STRICT);
回答by Sajjad Shirazy
if you wanna keep OOP form without turning any error off, you can also:
如果您想在不关闭任何错误的情况下保持 OOP 形式,您还可以:
class A
{
public function foo() {
;
}
}
class B extends A
{
/*instead of :
public function foo($a, $b, $c) {*/
public function foo() {
list($a, $b, $c) = func_get_args();
// ...
}
}
回答by Psychokiller1888
When you are using the same function in a parent class and a child class, but the child class needs parameters while the parent one not, you'll get the Strict Standards
error.
当您在父类和子类中使用相同的函数,但子类需要参数而父类不需要时,您将收到Strict Standards
错误消息。
Example
例子
Manager:
经理:
public function getAtPosition($position)
{
foreach ($this->getList() as $obj)
{
if ($obj->getPosition() == $position)
return $obj;
}
return null;
}
MenuManager extends Manager:
MenuManager 扩展了 Manager:
public function getAtPosition($position, $parent)
{
foreach ($this->getList() as $m)
{
if ($m->getParent() == $parent && $m->getPosition() == $position)
return $m;
}
return null;
}
This example will generate an error:
这个例子会产生一个错误:
Strict standards: Declaration of MenuManager::getAtPosition() should be compatible with Manager::getAtPosition($position)
严格的标准:MenuManager::getAtPosition() 的声明应该与 Manager::getAtPosition($position) 兼容
Because we don't have the same arguments to the function, so let's trick this and add arguments, even though we're not using them!
因为我们没有相同的函数参数,所以让我们欺骗这个并添加参数,即使我们没有使用它们!
Manager:
经理:
public function getAtPosition($position, $dummy = 0) // Dummy to avoid Strict standards errors
{
foreach ($this->getList() as $obj)
{
if ($obj->getPosition() == $position)
return $obj;
}
return null;
}
MenuManager extends Manager:
MenuManager 扩展了 Manager:
public function getAtPosition($position, $parent = 0)
{
foreach ($this->getList() as $m)
{
if ($m->getParent() == $parent && $m->getPosition() == $position)
return $m;
}
return null;
}
Only one to be careful is that when using getAtPosition()
from MenuManager.class.php
, be sure you are actually sending 2 parameters, as we have to declare $parent = 0
in order to match the parent's declaration.
唯一需要注意的是,在使用getAtPosition()
from 时MenuManager.class.php
,请确保您实际上发送了 2 个参数,因为我们必须声明$parent = 0
以匹配父级的声明。
Every class extending Manager
and not containing getAtPosition()
will use the method from Manager
.
每个扩展Manager
和不包含的类getAtPosition()
都将使用来自 的方法Manager
。
If declared in a child class, php will use the method from the child class instead of the parent's one. There is no overloading
in PHP, so that is how I worked around it until it is properly implemented.
如果在子类中声明,php 将使用子类中的方法而不是父类的方法。overloading
PHP中没有,所以这就是我如何解决它,直到它被正确实现。
回答by T.Todua
Here is a better answer - https://stackoverflow.com/a/9243127/2165415
这是一个更好的答案 - https://stackoverflow.com/a/9243127/2165415
for example,
parentClass::customMethod($thing = false)
and
childClass::customMethod($thing)
so, when you call customMethod()
for the class, it may trigger the error, because the child's method hasn't defined a default value for the first argument.
例如,
parentClass::customMethod($thing = false)
和
childClass::customMethod($thing)
因此,当你调用customMethod()
的类,可能引发错误,因为孩子的方法还没有定义的第一个参数的默认值。