PHP子类可以改变重写方法的参数吗?

Kim*_*nce 6 php oop

我可以覆盖子类中的PHP方法并更改签名中的参数,如下所示.

class theParent {
  function myMethod($param1) {
    // code here
  }
}

class theChild extends theParent {
  function myMethod($param1, $param2) {
    // code here
  }
}
Run Code Online (Sandbox Code Playgroud)

我测试了它,它工作正常,不会引起任何错误.我的问题是,这是不好的形式?还是OOP的基本原则?

如果父方法被声明为abstract,则子签名不能偏离.据推测,如果您需要强制执行界面的这一方面,这是使用的机制?

Mat*_*att -1

只要

class theChild extends theParent {
}
Run Code Online (Sandbox Code Playgroud)

这是 OOP 的一个很好的例子。