adding parameters to overridden method E_STRICT observation

前端 未结 2 950
独厮守ぢ
独厮守ぢ 2021-01-17 23:52

It appears (PHP 5.3) that if you are overriding a class method, it is okay to you can add additional parameters, as long as they have default values.

For

相关标签:
2条回答
  • 2021-01-18 00:18

    This is not a bug and is acceptable PHP programming practise.

    Please note that multiple overrides can cause programmer headaches though and should be avoided where possible.

    Alternatively I usually either have a single extended class to override per class or just overload a class method within the actual class itself.

    0 讨论(0)
  • 2021-01-18 00:19

    If you add a non-defaulted parameter to an overridden method, the subclass no longer satisfies the contract defined by the superclass. You cannot correctly call test2->stuff(), because this method now expects a parameter - but the superclass says you should be able to call it without one. Hence the E_STRICT warning.

    If you add a defaulted parameter though, you can still call test3->stuff() (from your example) - as the superclass expects - and so the contract is not broken. In fact, by adding the optional parameter, you have simply extended it.

    0 讨论(0)
提交回复
热议问题