php - 无法声明在父类(super class)的接口(interface)中声明的子类方法

标签 php inheritance interface propel overloading

这应该是一个非常简单的类和接口(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/

相关文章:

php - 如何在 PHP 中获取特定周的天数?

node.js - 如何 "subclass"Node.js 模块

jquery-ui - JQuery ui 小部件继承

objective-c - iOS - 不能使用 'super' 作为引用?

c# - 奇怪的 C# 编译器错误 : cyclic inheritance

c++ - 我如何声明指向共享公共(public)接口(interface)的不同类的指针?

delphi - 如何在运行时获取接口(interface)的名称?

php - 使用 CodeKit 和 haml/PHP

php - 我的 SQL 计数返回许多 "1"实例

javascript - 根据服务器响应 vai php 更改 ajax 成功时的变量值