没有 OOP 的观察者模式逻辑?

发布于 2024-09-11 19:47:14 字数 268 浏览 6 评论 0 原文

我正在考虑在我的网站上实现类似于观察者模式的逻辑,以实现挂钩。

我正在寻找类似于此 允许的最佳方式PHP 应用程序的插件

但是,那里的代码太有限,因为我无法将多个钩子附加到同一个侦听器。

我对如何放大该代码以使其能够在一个事件中侦听多个操作一无所知。

谢谢

I was thinking about implementing a logic similar to observer pattern on my website, for implementing hooks.

What I am looking for is something similar to this Best way to allow plugins for a PHP application

However the code there is too limited, as I cant attach multiple hooks to same listener.

I am clueless about how to amplify that code to make it able to listen multiple actions at one event.

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的背包 2024-09-18 19:47:14

您可以按照 ircmaxell 的建议进行操作:添加挂钩。但显然,他提供的信息对你来说还不够。

如果您喜欢通过示例学习,您可以查看 CMS Drupal,它不是 OOP,而是使用观察者模式,称为 钩子遍布各处,以实现模块化设计。

钩子的工作原理如下:

  • 一段 php 查找是否存在专门命名的函数。
  • 如果存在,则调用它并使用其输出(或不对其执行任何操作)

例如:

  • 在 Drupal 中保存文章之前,文章系统会调用 hook_insert
  • 每个具有名称为 ModuleName_insert 的函数的模块都会看到该函数被调用。示例:pirate.module 可能有一个函数pirate_insert()。文章系统沿着所有模块进行往返并查看 ModuleName_insert 是否存在。它将经过pirate模块并找到pirate_insert()。然后它将调用该函数(并传递一些参数)。因此,允许 Pirate.module 在插入之前更改文章(或触发一些操作,例如将正文文本转换为盗版语音)。

神奇的事情发生在所谓的 user_callbacks 中。 示例

$hook = 'insert'
foreach (module_implements($hook) as $module) {
  $function = $module .'_'. $hook;
  $result = call_user_func_array($function, $args);
}

函数 module_implements 可能看起来像这样的东西:

$list = module_list(FALSE, TRUE, $sort); //looks for files that are considered "modules" or "addons".
foreach ($list as $module) {
  if (function_exists($module.'_'.$hook)) { //see if the module has the hook 'registered'
    $implementations[$hook][] = $module; //if so: add it to a list with functions to be called.
  }
}

You can do as ircmaxell suggests: add hooks. But clearly, the information he gave was not enough for you.

If you like learning by example, you may look at the CMS Drupal, wich is not OOP, but uses the observer pattern, called hooks all over the place to allow a modular design.

A hook works as follows:

  • a piece of php looks for the existence of a specially named function.
  • If that exists, call it and use its output (or do nothing with it)

For example:

  • Just before an article gets saved in Drupal, the article-system calls the hook_insert
  • Every module that has a function in the name of ModuleName_insert, will see that function being called. Example: pirate.module may have a function pirate_insert(). The article system makes a roundtrip along all the modules and sees if ModuleName_insert exists. It will pass by pirate module and finds pirate_insert(). It will then call that function (and pass some arguments along too). As such, allowing the pirate.module to change the article just before insertation (or fire some actions, such as turning the body-text into pirate-speek).

The magic happens in so called user_callbacks. An example:

$hook = 'insert'
foreach (module_implements($hook) as $module) {
  $function = $module .'_'. $hook;
  $result = call_user_func_array($function, $args);
}

And the function module_implements might look something like:

$list = module_list(FALSE, TRUE, $sort); //looks for files that are considered "modules" or "addons".
foreach ($list as $module) {
  if (function_exists($module.'_'.$hook)) { //see if the module has the hook 'registered'
    $implementations[$hook][] = $module; //if so: add it to a list with functions to be called.
  }
}
梦过后 2024-09-18 19:47:14

只需添加一个“*”挂钩,然后修改 hook() 函数即可调用指定事件和“*”事件中的所有“挂钩”。

然后,只需执行以下操作:

add_listener('*', 'mycallback');

Simply add a ' * ' hook, and modify the hook() function to call all the 'hooks' in both the named event and the ' * ' event.

Then, simply do:

add_listener('*', 'mycallback');
老子叫无熙 2024-09-18 19:47:14

看一下 Spl_Observer

您说过您不需要 OOP,但您可以轻松地实现一个非 OOP 包装器。

Take a look at Spl_Observer.

You said you didn't want OOP, but you can easily implement a non-OOP wrapper around this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文