下面是我的文件夹结构。我需要访问WordPress主题控制器中的类。
控制器->WP\\U Db\\U连接。php
控制器->WP\\U产品。php
示例插件。php连接。php
class WP_Db_connection {
public $dbConfig = [];
public $errors = [];
// Constructor
function __construct() {
self::init_db()
}
function init_db () {
// Code to return DB connection
}
}
new WP_Db_connection();
->WP\\U产品。phprequire_once ROOT_PATH.\'/WP_Db_connection.php\';
class WP_Product{
function __construct(){
echo \'Inside Products\';
}
}
new WP_Product();
->samplePlugin。phpspl_autoload_register ( "autoload", true, true );
function autoload($class){
try{
$classPath = ROOT_PATH.\'/controllers/\'.str_replace ( \'\\\\\', \'/\', $class ) . \'.php\';
if($classPath && file_exists($classPath)){
require_once $classPath;
}
} catch (Exception $e){
echo \'<br>Exception caught: \'.$e;
}
}
class WP_main {
// Code for class
}
new WP_main();
在下面的主题代码中,WP\\u main存在,但其他类不存在。echo \'WP_main: \'.class_exists(\'WP_main\');
echo \',WP_Product: \'.class_exists(\'WP_Product\');
echo \',WP_Db_connection: \'.class_exists(\'WP_Db_connection\');
输出:WP_main: 1,WP_Db_connection: ,WP_Db_connection: