类与插件框架冲突 - PHP / Wordpress
我正处于编写基本 MVC WordPress 插件开发框架的开始阶段,我意识到我将遇到命名冲突问题。如果我想将此框架包含在我正在开发的几个插件和/或主题中,最终我将遇到 class_exists() 无法解决的问题。
我可以将框架创建为独立插件,但这将要求下载我的插件或主题之一的任何人也下载该框架,这似乎不现实(特别是对于没有此类插件的现有插件的升级)目前是一个依赖项)。
不管怎样,我想你们中的许多人可能都遇到过同样的问题,我想看看是否有人制定了一个好的策略来解决这个问题。
如果可能的话,我不需要每个插件都有一个唯一的前缀(这将使更新框架成为一场噩梦)。我希望有一些聪明的方法来动态命名每个类,而不必对其进行硬编码。
I'm in the beginning stages of writing a basic MVC WordPress plugin development framework, and I'm realizing I'm going to have a naming conflict issue. If I want to include this framework in several of the plugins and/or themes I'm developing, eventually I'm going to run into an issue that class_exists() isn't going to solve.
I could create the framework as a stand-alone plugin, but that would require anyone who downloaded one of my plugins or themes to also download the framework, which doesn't seem realistic (especially for upgrades to existing plugins that don't have such a dependency currently).
Anyway, I figured many of you out there have probably run into these same issues, and I wanted to see if anyone had developed a good strategy to manage the problem.
If possible, I don't what to have to have a unique prefix per plugin (that will making updating the framework a nightmare). I'm hoping there's some clever way to dynamically name each of these classes without having to hard code it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
命名空间可以解决您的问题。
Namespace can resolve your problem.
更新
向提问者道歉,因为第一次没有得到正确的结果。吕克是对的。 命名空间是最好的选择。
原创
一些框架选择在类名前面放置一个字母以避免冲突。 Yii 只是将 C 放在前面,如下所示:
UPDATE
Apologies to the asker for not getting this right the first time. The Luc is right. Namespace is the best option.
Original
Some frameworks choose to put a letter in front of their class names to avoid conflicts. Yii just buts C in front like:
我最终选择的解决方案是创建一个 bootstrap.php 文件,让框架的每个实例注册(在全局变量中)它的版本及其文件路径。然后,我注册一个在加载所有插件/主题后运行的操作,该操作会比较所有版本并仅加载与最新版本关联的类。
我认为唯一的缺点是我必须确保我的框架向后兼容,但无论如何我计划这样做。
这是我在引导文件中使用的代码。显然,框架的每个实例中的版本号需要与所包含的框架的版本号相对应。
The solution I ended up choosing was to create a
bootstrap.php
file, have each instance of the framework register (in a global variable) what version it is and its file path. Then I register an action to run after all plugins/theme are loaded which compares all versions and only loads the classes associated with the most recent version.The only downside I see to this is that I'd have to make sure my framework is backwards compatible, but I planned on doing that anyway.
Here is the code I used in my bootstrap file. Obviously the version number in each instance of the framework would need to correspond to the version number of the framework included.