我有这个代码:
abstract class Base{
public function delete(){
// Something like this (id is setted in constructor)
$this->db->delete($this->id);
}
}
然后我有另一个扩展Base的类,例如:
class Subtitles extends Base{
public function delete($parameter){
parent::delete();
// Do some more deleting in transaction using $parameter
}
}
它恰好也有方法delete。
问题来了:
当我呼叫时
$subtitles->delete($parameter)
我得到了:
Strict error - Declaration of Subtitles::delete() should be compatible with Base::delete()
所以我的问题是,为什么我不能有不同参数的后代方法?
谢谢你的解释。
这是因为PHP做的是方法覆盖,而不是方法重载。所以方法签名必须完全匹配。
作为您问题的一个工作区域,您可以将基类上的删除重组为
public function delete($id = null){
// Something like this (id is setted in constructor)
if ($id === null) $id = $this->id;
$this->db->delete($id);
}
然后更改子类方法签名以匹配。
若要覆盖基类中的函数,方法必须具有与其替换的方法相同的"Signature"。
签名由名称、参数(以及参数顺序)和返回类型组成。
这是多态性的本质,也是面向对象编程获得很大力量的地方。如果不需要重写父方法,请为新方法指定一个不同的名称。
这本应该是对@orangePill的ansert的评论,但我没有足够的声誉来评论。
我在静态方法方面也遇到了同样的问题,我使用后期静态绑定进行了以下操作。也许这对某人有帮助。
abstract class baseClass {
//protected since it makes no sense to call baseClass::method
protected static function method($parameter1) {
$parameter2 = static::getParameter2();
return $parameter1.' '.$parameter2;
}
}
class myFirstClass extends baseClass {
//static value, could be a constant
private static $parameter2 = 'some value';
public static function getParameter2() {
return self::$parameter2;
}
public static function method($parameter1) {
return parent::method($parameter1);
}
}
class mySecondClass extends baseClass {
private static $parameter2 = 'some other value';
public static function getParameter2() {
return self::$parameter2;
}
public static function method($parameter1) {
return parent::method($parameter1);
}
}
使用
echo myFirstClass::method('This uses'); // 'This uses some value'
echo mySecondClass::method('And this uses'); // 'And this uses some other value'