【发布时间】: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,所以不要忽视我在这里很愚蠢的可能性。)
【问题讨论】:
-
该参数在一种情况下是可选的,而在另一种情况下则不是。几乎没有“相同”。