我正在扩展一个类,但在某些情况下,我重写了一个方法。有时在2个参数中,有时在3个,有时没有参数。PHP - 使用不同数量的参数覆盖函数
不幸的是我得到了一个PHP警告。
我的最低可验证例如: http://pastebin.com/6MqUX9Ui
<?php
class first {
public function something($param1) {
return 'first-'.$param1;
}
}
class second extends first {
public function something($param1, $param2) {
return 'second params=('.$param1.','.$param2.')';
}
}
// Strict standards: Declaration of second::something() should be compatible with that of first::something() in /home/szymon/webs/wildcard/www/source/public/override.php on line 13
$myClass = new Second();
var_dump($myClass->something(123,456));
我收到PHP错误/警告/信息:
如何防止这样的错误?
你不能因为它违反了LSP http://en.wikipedia.org/wiki/Liskov_substitution_principle –
请访问http:// stackoverflow.com/questions/3115388/ – Uberfuzzy