插件系统如何工作(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屋!