简体   繁体   English

PHP OOP 我无法理解的错误

[英]PHP OOP error that I cannot understand

I try to extend the CheckfrontAPI class with my new class.我尝试用我的新 class 扩展 CheckfrontAPI class。

In my case I use the Singleton pattern in order to load only one instance at a time of my class and I get that error在我的例子中,我使用 Singleton 模式,以便在我的 class 一次只加载一个实例,我得到了那个错误

Fatal error: Declaration of CheckFrontIntegrator::store() must be compatible with that of CheckfrontAPI::store() in /home/my_web_site/public_html/wp-content/plugins/checkfront/class/Checkfront_Integration.php on line 83致命错误:CheckFrontIntegrator::store() 声明必须与第 83 行 /home/my_web_site/public_html/wp-content/plugins/checkfront/class/Checkfront_Integration.php 中 CheckfrontAPI::store() 的声明兼容

Any idea on how to solve that issue?关于如何解决该问题的任何想法?

Here is the CheckfrontAPI source code: https://github.com/Checkfront/PHP-SDK/blob/master/lib/CheckfrontAPI.php这是 CheckfrontAPI 源代码: https://github.com/Checkfront/PHP-SDK/blob/master/lib/CheckfrontAPI.php

And here is my class that extends that class:这是我的 class,它扩展了 class:

<?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());
    }
}

?>

And I initiate the new instance of that class like that:然后我像这样启动 class 的新实例:

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

where args are all the important information needit by the class to initiate a new object其中 args 是 class 启动新的 object 所需的所有重要信息

AFTER EDIT编辑后

I have change my method store from:我改变了我的方法存储:

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

to

protected function store($data)
....

and the problem still occure:(问题仍然存在:(

CheckfrontAPI is an abstract class? CheckfrontAPI是抽象的class? in this case your CheckFrontIntegrator::store() arguments count must be identical to original declaration在这种情况下,您的 CheckFrontIntegrator::store() arguments 计数必须与原始声明相同

EDIT编辑

I see on github我在 github 上看到

abstract protected function store($data);

your override must be:您的覆盖必须是:

protected function store($data) {

}

You are extending CheckfrontAPI.您正在扩展 CheckfrontAPI。 CheckfrontAPI has a method store(). CheckfrontAPI 有一个方法 store()。 If you override that method you must do it properly.如果您覆盖该方法,则必须正确执行。

Post the code of CheckfrontAPI and your class Checkfront_Integration: when can understand what's the problem.贴出CheckfrontAPI的代码和你的class Checkfront_Integration: 什么时候才能明白是什么问题。

When you want to extent the functionality of an existing class by writing your own class and the class you are extending is is an abstract one, you'll need to make sure that the function calls are compatible.如果您想通过编写自己的 class 来扩展现有 class 的功能,而您要扩展的 class 是抽象的,则需要确保 function 调用兼容。
What does this mean?这是什么意思?

If the class you are extending has this function call for example:例如,如果您要扩展的 class 有此 function 调用:

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

Then you will have to honor the function signature in your implementation - that means you'll still have to have to pass two function arguments in your version.然后你将不得不在你的实现中尊重 function 签名 - 这意味着你仍然必须在你的版本中传递两个 function arguments 。

You will not be able to alter is to be like this:你将无法改变是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM