这应该是一个非常简单的类和接口(interface)问题,但在我展示示例时请耐心等待。
在 Propel ORM 库中,所有数据库表都被抽象为名为 BaseTablename
的类。在基本对象中定义了与数据库交互的各种方法。然后该库还生成以表命名的类,例如Tablename
,这对于重写基方法和添加自定义方法非常方便。
我只是想重写默认的 delete()
方法,以便能够删除一些相关数据。但是当我声明覆盖方法时,出现以下错误:
Fatal error: Declaration of Tablename::delete() must be compatible with that of Persistent::delete()
那么,考虑到以下基本定义,为什么我不能覆盖 delete()
方法?
/**
* Part of the Propel library
*/
interface Persistent {
public function delete (PropelPDO $con = null);
}
/**
* Generated by Propel
*/
class BaseTablename extends BaseObject implements Persistent {
public function delete (PropelPDO $con = null) {
doesImportantStuff();
}
}
/**
* Skeleton class is generated by Propel
*/
class Tablename extends BaseTablename {
/**
* MY OWN BEAUTIFUL [BUT BROKEN] CODE
*/
public function delete (PropelPDO $con = null) {
doMyOwnStuff();
// Added this 2011-05-30 -- As it happens, this IS the Problem!
// I needed to add the $con parameter to the call to preserve
// the "chain of compatibility", so to speak.
parent::delete();
}
}
更新:我添加了我的 parent::delete() 调用,我没有将其包含在我的原始示例代码中。这真的会让一切变得不同。对不起各位,非常感谢那些确认工作代码的人
答案 是我需要在所有声明和调用中保留参数。我的重载函数应该是:
public function delete (PropelPDO $con = null) {
doMyOwnStuff();
parent::delete($con);
}
最佳答案
此代码有效。问题必须在服务器副本或 PHP 版本中。 (我已经在 PHP 版本 5.3.3-7+squeeze1 上运行测试没有问题)
关于php - 无法声明在父类(super class)的接口(interface)中声明的子类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6146377/