3

開発中の 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 機能も使用していると思われる他のプラグインから大量のエラーが発生します。

何か案は?

4

1 に答える 1

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";

}
于 2012-08-06T21:47:06.703 に答える