[英]How to override WordPress Plugin class method
我有一個插件,它的類是這樣的:
<?php
namespace myPro\EventCalendarPro;
use WP_Widget;
/**
* Adds ECP_Widget widget.
*/
class ECP_Widget extends WP_Widget {
public function widget () {
}
}
?>
現在,如何從我的主題functions.php文件中覆蓋小部件方法?
這個問題的簡單答案是“你不能這樣做”。
因為在 PHP 中你不能重新定義類、方法和函數。 但是你可以使用PHP Runkit擴展來重新定義你的方法
重新定義方法的 Runkit 函數是runkit_method_redefine
<?php
class Example {
function foo() {
return "foo!\n";
}
}
// create an Example object
$e = new Example();
// output Example::foo() (before redefine)
echo "Before: " . $e->foo();
// Redefine the 'foo' method
runkit_method_redefine(
'Example',
'foo',
'',
'return "bar!\n";',
RUNKIT_ACC_PUBLIC
);
// output Example::foo() (after redefine)
echo "After: " . $e->foo();
?>
但是首先你應該在你的服務器上安裝php-runkit
擴展。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.