Class wordpress 插件中的自动加载器
Class autoloader in wordpress plugin
我想编写一个 class 自动加载器以在 wordpress 插件中使用。这个插件会安装在多个站点,我想尽量减少与其他插件冲突的机会。
自动加载器将是这样的:
function __autoload($name) {
//some code here
}
我的主要问题是,如果另一个 class 也使用这样的函数会怎样?我认为这一定会出问题。避免类似情况的最佳方法是什么?
我试图不使用名称空间,因此代码也适用于 php 的早期版本。
我建议使用命名空间和 PSR-4。您可以简单地复制 this autoloader example from FIG.
但如果你不想,你可以使用自动加载器 like this one,它定义了 WP classes 名称的约定,并使用它来查找 class文件。
因此,例如,如果您调用 'Main' class,则此自动加载器将尝试包含路径中的 class 文件:
<plugin-path>/class-main.php
像这样使用一些实现。
function TR_Autoloader($className)
{
$assetList = array(
get_stylesheet_directory() . '/vendor/log4php/Logger.php',
// added to fix woocommerce wp_email class not found issue
WP_PLUGIN_DIR . '/woocommerce/includes/libraries/class-emogrifier.php'
// add more paths if needed.
);
// normalized classes first.
$path = get_stylesheet_directory() . '/classes/class-';
$fullPath = $path . $className . '.php';
if (file_exists($fullPath)) {
include_once $fullPath;
}
if (class_exists($className)) {
return;
} else { // read the rest of the asset locations.
foreach ($assetList as $currentAsset) {
if (is_dir($currentAsset)) {
foreach (new DirectoryIterator($currentAsset) as $currentFile)
{
if (!($currentFile->isDot() || ($currentFile->getExtension() <> "php")))
require_once $currentAsset . $currentFile->getFilename();
}
} elseif (is_file($currentAsset)) {
require_once $currentAsset;
}
}
}
}
spl_autoload_register('TR_Autoloader');
基本上这个自动加载器已经注册并具有以下特点:
- 如果不遵循包含 classes (assetList) 的文件位置的特定模式,您可以添加特定的 class 文件。
- 您可以将整个目录添加到您的 class 搜索中。
- 如果已经定义了class,你可以添加更多的逻辑来处理它。
- 您可以在代码中使用条件 class 定义,然后在 class 加载器中覆盖您的 class 定义。
现在,如果您想以 OOP 方式进行操作,只需在 class 中添加自动加载器功能即可。 (即:myAutoloaderClass)并从构造函数中调用它。
然后只需在 functions.php
中添加一行
new myAutoloaderClass();
并在构造函数中添加
function __construct{
spl_autoload_register('TR_Autoloader' , array($this,'TR_Autoloader'));
}
希望这对您有所帮助。
人力资源
我想编写一个 class 自动加载器以在 wordpress 插件中使用。这个插件会安装在多个站点,我想尽量减少与其他插件冲突的机会。
自动加载器将是这样的:
function __autoload($name) {
//some code here
}
我的主要问题是,如果另一个 class 也使用这样的函数会怎样?我认为这一定会出问题。避免类似情况的最佳方法是什么?
我试图不使用名称空间,因此代码也适用于 php 的早期版本。
我建议使用命名空间和 PSR-4。您可以简单地复制 this autoloader example from FIG.
但如果你不想,你可以使用自动加载器 like this one,它定义了 WP classes 名称的约定,并使用它来查找 class文件。
因此,例如,如果您调用 'Main' class,则此自动加载器将尝试包含路径中的 class 文件:
<plugin-path>/class-main.php
像这样使用一些实现。
function TR_Autoloader($className)
{
$assetList = array(
get_stylesheet_directory() . '/vendor/log4php/Logger.php',
// added to fix woocommerce wp_email class not found issue
WP_PLUGIN_DIR . '/woocommerce/includes/libraries/class-emogrifier.php'
// add more paths if needed.
);
// normalized classes first.
$path = get_stylesheet_directory() . '/classes/class-';
$fullPath = $path . $className . '.php';
if (file_exists($fullPath)) {
include_once $fullPath;
}
if (class_exists($className)) {
return;
} else { // read the rest of the asset locations.
foreach ($assetList as $currentAsset) {
if (is_dir($currentAsset)) {
foreach (new DirectoryIterator($currentAsset) as $currentFile)
{
if (!($currentFile->isDot() || ($currentFile->getExtension() <> "php")))
require_once $currentAsset . $currentFile->getFilename();
}
} elseif (is_file($currentAsset)) {
require_once $currentAsset;
}
}
}
}
spl_autoload_register('TR_Autoloader');
基本上这个自动加载器已经注册并具有以下特点:
- 如果不遵循包含 classes (assetList) 的文件位置的特定模式,您可以添加特定的 class 文件。
- 您可以将整个目录添加到您的 class 搜索中。
- 如果已经定义了class,你可以添加更多的逻辑来处理它。
- 您可以在代码中使用条件 class 定义,然后在 class 加载器中覆盖您的 class 定义。
现在,如果您想以 OOP 方式进行操作,只需在 class 中添加自动加载器功能即可。 (即:myAutoloaderClass)并从构造函数中调用它。 然后只需在 functions.php
中添加一行new myAutoloaderClass();
并在构造函数中添加
function __construct{
spl_autoload_register('TR_Autoloader' , array($this,'TR_Autoloader'));
}
希望这对您有所帮助。 人力资源