事件驱动的架构和PHP中的钩子 [英] Event-driven architecture and hooks in PHP

查看:92
本文介绍了事件驱动的架构和PHP中的钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计划开发一个具有PHP后端以与数据存储库进行通信的游戏.我对此进行了思考,并得出结论,我们的游戏遵循的最佳设计范例是事件驱动的.我希望拥有一个成就系统(类似于该网站的徽章系统),并且基本上,我希望能够将这些成就检查"挂钩到游戏中发生的许多不同事件中.即:

I am planning on working on a game that has a PHP back-end to communicate with the data repository. I was thinking about it and concluded that the best design paradigm to follow for our game would be event driven. I am looking to have an achievement system (similar to the badges system of this website) and basically I would like to be able to hook these "achievement checks" into a number of different events that occur in the game. ie:

当用户执行操作X时,将钩子Y触发,并调用所有附加功能以核对成就要求.

When a user does action X hook Y is fired and all attached functions are called to check against an achievement requirement.

在构建这样的体系结构时,我将允许轻松添加新的成就,因为我要做的就是将检查功能添加到适当的钩子中,其他所有内容都将落入原处.

In structuring the architecture like this I will allow for new achievements to be added easily as all I will have to do is add the checking function to the proper hook and everything else will fall into place.

我不确定这是否是我打算做的很好的解释,但是无论如何我都在寻找以下内容:

I'm not sure if this is a great explanation of what I intend to do, but in any case I am looking for the following:

  1. 有关如何编写事件驱动的应用程序的良好参考材料
  2. 显示如何在PHP函数中放置挂钩"的代码段
  3. 显示如何将功能附加到第2点提到的挂钩"的代码段

对于如何完成2)和3),我有一些想法,但我希望对此事精通的人能为您提供一些最佳实践的机会.

I have a few ideas as to how to accomplish 2) and 3) but I was hoping that somebody well-versed in the matter could shed some light on best practices.

提前谢谢!

推荐答案

有关如何编写事件驱动的应用程序的良好参考材料

Good reference material on how to code an event-driven application

您可以使用哑"回调(或实施事件驱动的应用程序中经常使用的模式: 观察者模式 .

Or implementing a pattern often used in event-driven applications: Observer Pattern.

显示如何在PHP函数中放置钩子"的代码段

Code snippet(s) showing how to put a "hook" in a function in PHP

上面的手动链接(回调可以存储到变量中)和一些观察者模式的PHP代码示例.

The manual link above (callbacks can be stored into a variable) and some PHP code examples for the Observer Pattern.

这篇关于事件驱动的架构和PHP中的钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆