本文介紹了PHP鉤子系統怎么做?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
您如何在 PHP 應用程序中實現鉤子系統以在其執行之前或之后更改代碼.hookloader 類的基本架構如何用于 PHP CMS(甚至是簡單的應用程序).那么如何將其擴展為完整的插件/模塊加載器?
How do you impliment a hook system in a PHP application to change the code before or after it executes. How would the basic architecture of a hookloader class be for a PHP CMS (or even a simple application). How then could this be extended into a full plugins/modules loader?
(另外,有沒有關于 CMS 掛鉤系統的書籍或教程?)
(Also, are there any books or tutorials on a CMS hook system?)
推薦答案
您可以構建一個事件系統 簡單 或復雜,隨您的需要.
You can build an events system as simple or complex as you want it.
/**
* Attach (or remove) multiple callbacks to an event and trigger those callbacks when that event is called.
*
* @param string $event name
* @param mixed $value the optional value to pass to each callback
* @param mixed $callback the method or function to call - FALSE to remove all callbacks for event
*/
function event($event, $value = NULL, $callback = NULL)
{
static $events;
// Adding or removing a callback?
if($callback !== NULL)
{
if($callback)
{
$events[$event][] = $callback;
}
else
{
unset($events[$event]);
}
}
elseif(isset($events[$event])) // Fire a callback
{
foreach($events[$event] as $function)
{
$value = call_user_func($function, $value);
}
return $value;
}
}
添加事件
event('filter_text', NULL, function($text) { return htmlspecialchars($text); });
// add more as needed
event('filter_text', NULL, function($text) { return nl2br($text); });
// OR like this
//event('filter_text', NULL, 'nl2br');
那就這樣稱呼吧
$text = event('filter_text', $_POST['text']);
或者像這樣刪除該事件的所有回調
Or remove all callbacks for that event like this
event('filter_text', null, false);
這篇關于PHP鉤子系統怎么做?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!