在PHP应用程序中实现插件的设计模式 [英] Design pattern for implementing plugins in PHP applications
问题描述
我已经研究了观察者模式,它只是一个通知系统,不允许代码直接扩展应用程序。我正在使用一个简单的钩子系统,我想到了:
public function registerHook($ hookName,array $ params = array())
{
$ this-> hooks [] = $ hookName;
foreach($ this-> plugins as $ pluginName => $ hooks){
if(in_array($ hookName,$ hooks)){
$ plugin = new $ pluginName($ this,$ this-> view,$ this-> controller);
$ plugin-> {$ hookName}($ params);
}
}
}
这对我的目的很好但是如果有一个设计模式已经经过测试和验证了很多次,我很好奇,我只是重新创造了轮子。
在Silver Bullet意义上没有一致意见。对于已建立的模式,您有多个选项,如
仅举几例。
您所使用的取决于您,但您应确保系统架构支持模块化。看看这些幻灯片的一些想法
Is there a consensus on how plugins should be implemented in a PHP application?
I've looked into the observer pattern which comes close, it's really just a notification system and doesn't allow code to extend the application directly. I'm currently using a simple hook systems that I came up with:
public function registerHook($hookName, array $params = array())
{
$this->hooks[] = $hookName;
foreach ( $this->plugins as $pluginName => $hooks ) {
if ( in_array($hookName, $hooks) ) {
$plugin = new $pluginName($this, $this->view, $this->controller);
$plugin->{$hookName}($params);
}
}
}
This works well for my purposes but I'm curious if there's a design pattern out there that has been tested and proven many times over and I'm just re-inventing the wheel.
There is no consensus as in the Silver Bullet sense. For established patterns, you have multiple options like
to name a few.
Which you use is up to you, but you should make sure your system architecture supports the modularity. Have a look at these slides for some ideas
这篇关于在PHP应用程序中实现插件的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!