快速放入,我试图建立一个使用WordPress的'生态系统',我有一个核心插件,然后额外的插件插件。WordPress PSR-4名称空间插件访问另一个命名空间插件
更深入的是,每个附加插件都需要核心插件才能运行。我已经使用WordPress标准编码和文件结构实践实现了这一点。我重做这个项目现在使用命名空间PSR-4,作曲家,闺房,然后通过作曲家
"autoload": {
"psr-4": {
"CorePlugin\\Library\\": "library"
}
}
等
标准的WordPress安装
|
|__www
|
|___wp-admin
|
|___wp-content
| |
| |___plugins
| | |
| | |___my-core-plugin
| | | |
| | | |___library
| | | | |
| | | | |___class-post-register.php
| | | |
| | | |___vendor
| | | | |
| | | | |___autoload.php
| | | |
| | | |___composer.json
| | | |
| | | |___core.php
| | |
| | |___my-first-addon-plugin
| | | |
| | | |___library
| | | |
| | | |___vendor
| | | | |
| | | | |___autoload.php
| | | |
| | | |___composer.json
| | | |
| | | |___core.php
| | |
| | |___my-second-addon-plugin
| | | |
| | | |___library
| | | |
| | | |___vendor
| | | | |
| | | | |___autoload.php
| | | |
| | | |___composer.json
| | | |
| | | |___core.php
| | |
| |___themes
| | |
| | |___my-custom-theme
| |
| wp-includes
核心插件PSR4示例核心插件类
<?php
namespace CorePlugin\library;
class Post_Register {
private __construct() {
// ... code
}
private init() {
}
private register($data) {
// .. code to register a custom post for example.
}
}
首先通过的作曲家的附加插件
下面附加的插件PSR4
"autoload": {
"psr-4": {
"FirstAddon\\Library\\": "library"
}
}
类是我很困惑。我想在不同的命名空间使用一个类从核心插件,我得到的错误:
Fatal error: Class 'CorePlugin\Library\Post_Register' not found in...
两个插件自动加载各自的作曲家生成自动加载的文件,所以我虽然我能够use
命名空间。在我深入研究PHP手册的这一部分(http://php.net/manual/en/language.namespaces.php)之前,我来这里问了一下,我可能会尝试子命名空间。
<?php
namespace FirstAddon;
use CorePlugin\Library\Post_Register;
class First_Addon {
private __construct() {
// ... code
}
private init() {
}
private another_function() {
}
}
而且,我很犹豫使用子命名空间与括号中,因为,例如,在laravel,use
FOO \吧;和use
bar \ foo;像这样。
<?php namespace App\Services;
use App\User;
use Validator;
use Illuminate\Contracts\Auth\Registrar as RegistrarContract;
class Registrar implements RegistrarContract {