类与插件框架冲突 - PHP / Wordpress

发布于 2024-11-03 21:27:05 字数 342 浏览 2 评论 0原文

我正处于编写基本 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 技术交流群。

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

发布评论

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

评论(3

软甜啾 2024-11-10 21:27:05

命名空间可以解决您的问题。

Namespace can resolve your problem.

寒江雪… 2024-11-10 21:27:05

更新

向提问者道歉,因为第一次没有得到正确的结果。吕克是对的。 命名空间是最好的选择。

原创

一些框架选择在类名前面放置一个字母以避免冲突。 Yii 只是将 C 放在前面,如下所示:

class CClassName 
{
}

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:

class CClassName 
{
}
幸福丶如此 2024-11-10 21:27:05

我最终选择的解决方案是创建一个 bootstrap.php 文件,让框架的每个实例注册(在全局变量中)它的版本及其文件路径。然后,我注册一个在加载所有插件/主题后运行的操作,该操作会比较所有版本并仅加载与最新版本关联的类。

我认为唯一的缺点是我必须确保我的框架向后兼容,但无论如何我计划这样做。

这是我在引导文件中使用的代码。显然,框架的每个实例中的版本号需要与所包含的框架的版本号相对应。

// register this version of the framework
$GLOBALS['pw_framework_meta']['0.1.2'] = __FILE__;

if ( !function_exists('pw_framework_init') ) {

    /**
     * Sort the $GLOBALS['pw_framework_meta'] variable for the latest registered
     * version of the framework. Include the PW_Framework.php file and then call init()
     */
    function pw_framework_init()
    {
        // get all the different versions registered with $GLOBALS['pw_framework_meta']
        $versions = $GLOBALS['pw_framework_meta'];

        // sort the versions
        uksort($versions, 'version_compare');

        // get the latest versions (the last in the array)
        $latest = end($versions);

        if ( !class_exists('PW_Framework') ) {
            require_once( dirname($latest) . '/PW_Framework.php' );
        }
        PW_Framework::init();
    }

    // register pw_framework_init() with the 'after_setup_theme' hook
    // This way we can ensure that all plugins or themes that might be using PW_Framework
    // have had a chance to register with the $GLOBALS['pw_framework_meta'] variable
    add_action( 'after_setup_theme', 'pw_framework_init' );
}

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.

// register this version of the framework
$GLOBALS['pw_framework_meta']['0.1.2'] = __FILE__;

if ( !function_exists('pw_framework_init') ) {

    /**
     * Sort the $GLOBALS['pw_framework_meta'] variable for the latest registered
     * version of the framework. Include the PW_Framework.php file and then call init()
     */
    function pw_framework_init()
    {
        // get all the different versions registered with $GLOBALS['pw_framework_meta']
        $versions = $GLOBALS['pw_framework_meta'];

        // sort the versions
        uksort($versions, 'version_compare');

        // get the latest versions (the last in the array)
        $latest = end($versions);

        if ( !class_exists('PW_Framework') ) {
            require_once( dirname($latest) . '/PW_Framework.php' );
        }
        PW_Framework::init();
    }

    // register pw_framework_init() with the 'after_setup_theme' hook
    // This way we can ensure that all plugins or themes that might be using PW_Framework
    // have had a chance to register with the $GLOBALS['pw_framework_meta'] variable
    add_action( 'after_setup_theme', 'pw_framework_init' );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文