我已经为这个需求创建了一个类,它与PSR-4兼容并遵循Wordpress编码标准。
您可以在此处访问:https://github.com/pablo-pacheco/wp-namespace-autoloader
解释就在这里,但基本上这是作曲家的依赖关系。您只需在项目中要求它:
"require": {
"pablo-pacheco/wp-namespace-autoloader": "dev-master"
}
然后给全班打电话
<?php
new \\WP_Namespace_Autoloader( array(
\'directory\' => __DIR__, // Directory of your project. It can be your theme or plugin. __DIR__ is probably your best bet.
\'namespace\' => __NAMESPACE__, // Main namespace of your project. E.g My_Project\\Admin\\Tests should be My_Project. Probably if you just pass the constant __NAMESPACE__ it should work
\'classes_dir\' => \'src\', // (optional). It is where your namespaced classes are located inside your project. If your classes are in the root level, leave this empty. If they are located on \'src\' folder, write \'src\' here
) );