PHP - 具有不同参数数的覆盖函数
2022-08-30 20:36:57
我正在扩展一个类,但在某些情况下,我正在重写一个方法。有时在2个参数中,有时在3个参数中,有时没有参数。
不幸的是,我收到一个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 错误/警告/信息:
如何防止此类错误?