非破坏性的spl_autoload_register

时间:2012-08-06 17:27:13

标签: php wordpress wordpress-plugin

我想在我正在开发的wordpress插件中通过spl_autoload_register使用动态加载类,但问题是它不能干扰此功能的预先存在的实现。在我最初的尝试中:

// register an autoloader function for template classes
spl_autoload_register ( 'template_autoloader' );

function template_autoloader ( $class ) {
    include LG_FE_DIR . "/includes/chart_templates/class.{$class}.php";
}

似乎可以加载我自己的类,但同时导致其他插件的大量错误,显然也使用了spl_autoload_register功能。

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

我相信我现在有一个合理的解决方案,但对其他人的意见持开放态度。我的解决方案是首先对模块的存在进行模板加载器功能测试。我还选择继续挂钩到“init”钩子,因此首先加载静态包含/需要(或至少大部分加载)。该功能现在看起来像这样:

add_action ( 'init' , 'class_loader' );

function class_loader () {
    // register an autoloader function for template classes
    spl_autoload_register ( 'template_autoloader' );
}

function template_autoloader ( $class ) {
if ( file_exists ( LG_FE_DIR . "/includes/chart_templates/class.{$class}.php" ) ) 
    include LG_FE_DIR . "/includes/chart_templates/class.{$class}.php";

}