子类中重新定义的方法与父类不同会出现错误(严格标准)
我收到此错误:
PayPal::raiseError() 声明 应该与 PEAR::raiseError()
分别是 PEAR::raiseError() 和 PayPal::raiseError():
function &raiseError($message = null,
$code = null,
$mode = null,
$options = null,
$userinfo = null,
$error_class = null,
$skipmsg = false)
{
class PayPal extends PEAR
{
function raiseError($message, $code = null)
{
return parent::raiseError($message, $code, null, null, null, 'PayPal_Error');
}
有什么方法可以使其在不修改定义的情况下工作吗?
我在此处阅读了有关类加载顺序的信息。这可能是问题所在吗?
I'm getting this error:
Declaration of PayPal::raiseError()
should be compatible with that of
PEAR::raiseError()
These are PEAR::raiseError() and PayPal::raiseError() respectively:
function &raiseError($message = null,
$code = null,
$mode = null,
$options = null,
$userinfo = null,
$error_class = null,
$skipmsg = false)
{
class PayPal extends PEAR
{
function raiseError($message, $code = null)
{
return parent::raiseError($message, $code, null, null, null, 'PayPal_Error');
}
Any way to make it work without modifying the definitions?
I read here about the order the class are loaded. Could that be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该问题与加载类的顺序无关。由于
PayPal
扩展了PEAR
,因此任何采用PEAR
对象作为参数的函数实际上都可能会被赋予一个PayPal
对象。由于PEAR
方法raiseError()
允许最多七个参数,因此PayPal
方法也应至少允许最多七个参数。最好的解决方案是重构
PayPal
中的raiseError()
:The problem has nothing to do with the order in which classes are loaded. Since
PayPal
extendsPEAR
, any function that would take aPEAR
object as parameter actually might be given aPayPal
object. And since thePEAR
methodraiseError()
allows up to seven parameters, thePayPal
method should also allow at least up to seven parameters.The best solution would be to refactor
raiseError()
inPayPal
:子类方法的签名(参数)需要与父类方法相同。这是因为 PHP 不允许重载具有不同参数数量或类型的函数。
您始终可以将
raiseError
方法重命名为不同的名称,例如raiseErrorWrapper
来回避此问题。The signature (parameters) of the child class method need to be the same as the parent method. This is because PHP does not allow overloading functions with varying parameter counts or types.
You could always just rename your
raiseError
method to something different, likeraiseErrorWrapper
to sidestep this issue.