我无法理解的 PHP OOP 错误

  • 本文关键字:PHP OOP 错误 php oop
  • 更新时间 :
  • 英文 :


我尝试用我的新类扩展CheckfrontAPI类。

就我而言,我使用单例模式,以便一次只加载我的类的一个实例,但出现该错误

致命错误:CheckFrontIntegrator::store() 的声明必须与第 83 行/home/my_web_site/public_html/wp-content/plugins/checkfront/class/Checkfront_Integration.php 中的 CheckfrontAPI::store() 的声明兼容

关于如何解决这个问题的任何想法?

以下是 CheckfrontAPI 源代码: https://github.com/Checkfront/PHP-SDK/blob/master/lib/CheckfrontAPI.php

这是我扩展该类的类:

<?php
class CheckFrontIntegrator extends CheckfrontAPI
{
    private static $instance = null;
    public $tmp_file = '.checkfront_oauth';
    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;
}
    public function session($session_id, $data = array())
    {
        $_SESSION['checkfront']['session_id'] = $session_id;
}
    public static function instance($data)
    {
        if(!isset(self::$instance))
        {
            self::$instance = new CheckFrontIntegrator($data);
        }
        return self::$instance;
    }
    public function __construct($data)
    {
        if(session_id() == '')
        {
            session_start();
        }
        parent::__construct($data, session_id());
    }
}
?>

我像这样启动该类的新实例:

$this->checkfront_integrator = CheckFrontIntegrator::instance($args);

其中 args 是类启动新对象所需的所有重要信息

编辑后

我已将方法存储从:

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

protected function store($data)
....

并且问题仍然出现:(

CheckfrontAPI 是一个抽象类? 在这种情况下,您的 CheckFrontIntegrator::store() 参数计数必须与原始声明相同

编辑

我在 github 上看到

abstract protected function store($data);

您的覆盖必须是:

protected function store($data) {
}

您正在扩展 CheckfrontAPI。CheckfrontAPI 有一个 method store()。如果重写该方法,则必须正确执行此操作。

发布 CheckfrontAPI 的代码和你的类Checkfront_Integration:什么时候可以了解问题所在。

当您想通过编写自己的类来扩展现有类的功能并且您要扩展的类是抽象类时,您需要确保函数调用是兼容的。
这是什么意思?

如果您要扩展的类具有此函数调用,例如:

function walk($direction, $speed = null);

然后,您必须在实现中遵循函数签名 - 这意味着您仍然必须在版本中传递两个函数参数。

你将无法改变是这样的:

function walk($direction, $speed, $clothing);

最新更新