【问题标题】:Class method signatures don't match?类方法签名不匹配?
【发布时间】:2013-12-22 19:49:06
【问题描述】:

我编写了一些代码来使用 PHP 库(对于名为 Checkfront 的产品,但这并不重要)。该库定义了一个具有抽象方法的类:

abstract protected function store($data);

在我的代码中,我将其子类化为

final protected function store($data=array()) {

在我的独立版本的代码中,使用一些示例代码中的实现可以正常工作:

/* DUMMY Data store.  This sample stores oauth tokens in a text file...
 * This is NOT reccomened in production.  Ideally, this would be in an encryped 
 * database or other secure source.  Never expose the client_secret or access / refresh
 * tokens.
 *
 * store() is called from the parent::CheckfrontAPI when fetching or setting access tokens.  
 *
 * When an array is passed, the store should save and return the access tokens.
 * When no array is passed, it should fetch and return the stored access tokens.
 *
 * param array $data ( access_token, refresh_token, expire_token )
 * return array
 */
final protected function store($data=array()) {
    $tmp_file = sys_get_temp_dir() . DIRECTORY_SEPARATOR. $this->tmp_file;
    if(count($data)  ) {
        file_put_contents($tmp_file,json_encode($data,true));
    } elseif(is_file($tmp_file)) {
        $data = json_decode(trim(file_get_contents($tmp_file)),true);
    }
    return $data;
}

。但是当我尝试在 Wordpress 插件中使用它时,我遇到了错误。这是 Wordpress 领域中的功能:

final protected function store($data=array()) {
    global $wpdb;
    if (count($data)) {
        $query = "
            INSERT INTO {$wpdb->prefix}pt_store (k, v)
            VALUES      (%s, %s)
            ON DUPLICATE KEY UPDATE v=values(v)";
        foreach ($data as $k => $v) {
            $wpdb->query($wpdb->prepare($query, $k, $v));
        }
    } else {
        $data = $wpdb->get_results("
            SELECT  (k, v)
            FROM    {$wpdb->prefix}pt_store", ARRAY_A);
    }
    return $data;
}

但是当我尝试激活插件时,它给了我错误:

Fatal error: Declaration of Checkfront::store() must be compatible with that of CheckfrontAPI::store() in /hsphere/local/home/[blah]/[blah].ixtrial.com/wordpress/wp-content/plugins/pt-checkfront/pt-checkfront.php on line 15

声明在我看来是一样的,所以有什么问题? (请记住,这是我第一次编写 PHP,所以不要忽视我在这里很愚蠢的可能性。)

【问题讨论】:

  • 该参数在一种情况下是可选的,而在另一种情况下则不是。几乎没有“相同”。

标签: php wordpress


【解决方案1】:

在 PHP 5.2 及更早版本下,可选参数将导致签名被视为不同。您无法以一种好的方式真正解决这个问题,所以请尝试升级 PHP。

'当然,您可能不需要默认值。在这种情况下,把它拿出来!你也可以通过null 并这样做:

final protected function store($data) {
    if ($data === null) $data = array();

【讨论】:

  • 托管网站,无法升级 PHP。我在我的家用电脑上进行命令行测试,当然它有更新版本的 PHP。
【解决方案2】:

添加=array() 使参数可选(请参阅this stackoverflow question)。

这会改变方法签名。

【讨论】:

  • 示例代码检查参数是否使用count($data) 给出。那么检查参数是否存在的等效方法是什么?
  • 添加=array()仅仅意味着$data的默认值是一个数组。 count($data) 对字符串同样有效。
【解决方案3】:

为避免此错误,您需要确保两个类中的方法签名相同,包括可选参数。

更改抽象类以允许使用默认数组

abstract protected function store($data = array());

PHP manual 状态

方法的签名必须匹配,即类型提示和所需参数的数量必须相同。例如,如果子类定义了一个可选参数,而抽象方法的签名没有,则签名中没有冲突。

【讨论】:

  • 您的报价与您的回答相矛盾。 “例如,如果子类定义了一个可选参数,而抽象方法的签名没有,则签名中没有冲突。”
猜你喜欢
  • 2018-01-15
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
相关资源
最近更新 更多