我可以重写子类中的PHP方法,并更改签名中的参数,如下所示。PHP子类可以改变重写方法的参数吗?
class theParent {
function myMethod($param1) {
// code here
}
}
class theChild extends theParent {
function myMethod($param1, $param2) {
// code here
}
}
我测试了这一点,它工作正常,并不会引发任何错误。我的问题是,这是不好的形式?还是OOP的基本原则?
如果父方法声明为抽象,则子签名不能偏离。据推测,如果你需要强制执行界面的这个方面,这是使用的机制?
这孩子上课是不是第一类的子类。更何况这是无效的语法... – nickb 2012-07-26 21:49:25
它被称为**重写**。如果您想阻止子类重写某个方法,请使用[final](http://php.net/manual/en/language.oop5.final.php)关键字。 – 2012-07-26 22:33:04