插件系统如何工作(wordpress,mybb ...)? [英] How does plugin system work (wordpress, mybb ...)?
问题描述
我很好奇插件的工作方式,我只知道我们不使用插件来更改代码,而是如何在不更改代码的情况下完成工作?编码人员在编码新项目以使其具有插件时应考虑什么?非常感谢你:D
I'm curious how plugins work, I just know that instead of changing the code we use plugins, but how do they do their job without changing the code ? and what should a coder consider when coding a new project so it can have plugins ? and thank you very much :D
推荐答案
关于如何实现插件系统的方法有多种. WordPress使用一种非常常见的方案,通常称为挂钩".我不知道确切的实现,但是它基本上是这样的:
There are multiple variations on how to implement a plugin system. Wordpress uses a quite common scheme often described as "hooks." I don't know the exact implementation but it basically works like this:
// plugin.php script registers its own callback function
register_plugin("hook_type", "plugin_function_123");
function plugin_function_123($params) { ... }
hook_type
通常是动作名称或其他名称.当主应用程序运行到特定点(或需要处理某些数据)时,它将调用所有已注册的回调函数:
Where the hook_type
is often an action name or something. And when the main application runs through a specific point (or e.g. needs some data processsed) it invokes all registered callback functions:
$output = call_plugins("hook_type", $param1, $param2);
这通常作为简单的循环在幕后实现:
This is often implemented behind the scenes as a simple loop:
foreach ($registered_plugins[$action] as $func) {
$func($param1, $param2, ...); // or call_user_func_
}
现在,取决于挂钩/操作类型,存在哪些参数,以及是否需要任何结果文本.参数传递也有差异(例如,某些回调需要& $ var引用).而且某些插件系统改为依赖对象(如果不存在那么多种不同的动作类型,或者要使用更复杂的结构).
Now it depends on the hook/action type what parameters are present, and if any result text is expected. There are also differences in parameter passing (e.g. some callbacks require &$var references). And some plugin systems rely on objects instead (if not as many varying action types exist or more complex structures are to be worked with).
这篇关于插件系统如何工作(wordpress,mybb ...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!