[英]PHP OOP error that I cannot understand
我尝试用我的新 class 扩展 CheckfrontAPI class。
在我的例子中,我使用 Singleton 模式,以便在我的 class 一次只加载一个实例,我得到了那个错误
致命错误: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
这是我的 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());
}
}
?>
然后我像这样启动 class 的新实例:
$this->checkfront_integrator = CheckFrontIntegrator::instance($args);
其中 args 是 class 启动新的 object 所需的所有重要信息
编辑后
我改变了我的方法存储:
final protected function store($data = array())
....
到
protected function store($data)
....
问题仍然存在:(
CheckfrontAPI是抽象的class? 在这种情况下,您的 CheckFrontIntegrator::store() arguments 计数必须与原始声明相同
编辑
我在 github 上看到
abstract protected function store($data);
您的覆盖必须是:
protected function store($data) {
}
您正在扩展 CheckfrontAPI。 CheckfrontAPI 有一个方法 store()。 如果您覆盖该方法,则必须正确执行。
贴出CheckfrontAPI的代码和你的class Checkfront_Integration: 什么时候才能明白是什么问题。
如果您想通过编写自己的 class 来扩展现有 class 的功能,而您要扩展的 class 是抽象的,则需要确保 function 调用兼容。
这是什么意思?
例如,如果您要扩展的 class 有此 function 调用:
function walk($direction, $speed = null);
然后你将不得不在你的实现中尊重 function 签名 - 这意味着你仍然必须在你的版本中传递两个 function arguments 。
你将无法改变是这样的:
function walk($direction, $speed, $clothing);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.