ale*_*rdy 15 php hook plugins module content-management-system
如何在PHP应用程序中实现钩子系统在执行之前或之后更改代码.如何将钩子类的基本体系结构用于PHP CMS(甚至是简单的应用程序).那怎么可以扩展到一个完整的插件/模块加载器?
(另外,CMS挂钩系统上有任何书籍或教程吗?)
Xeo*_*oss 31
您可以根据需要构建简单或复杂的事件系统.
/**
* 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;
}
}
Run Code Online (Sandbox Code Playgroud)
添加活动
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');
Run Code Online (Sandbox Code Playgroud)
然后这样称呼它
$text = event('filter_text', $_POST['text']);
Run Code Online (Sandbox Code Playgroud)
或者像这样删除该事件的所有回调
event('filter_text', null, false);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9832 次 |
| 最近记录: |