简体   繁体   中英

PHP OOP error that I cannot understand

I try to extend the CheckfrontAPI class with my new 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

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

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

And here is my class that extends that 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:

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

where args are all the important information needit by the class to initiate a new 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? in this case your CheckFrontIntegrator::store() arguments count must be identical to original declaration

EDIT

I see on github

abstract protected function store($data);

your override must be:

protected function store($data) {

}

You are extending CheckfrontAPI. CheckfrontAPI has a method 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.

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.
What does this mean?

If the class you are extending has this function call for example:

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.

You will not be able to alter is to be like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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