我有以下文件结构
- esimowl-mobimatter
- - src
- - - Esimowl
- - - - Mobimatter
- - - - - Product.php
- - composer.json
- - esimowl-mobimatter.php
我的composer.json文件有以下内容
{
"name": "esimowl/mobimatter",
"description": "An awesome plugin created by Esimowl",
"type": "wordpress-plugin",
"autoload": {
"psr-4": {
"Esimowl\\Mobimatter\\": "./src/"
}
}
}
请注意我已经在我的composer.json中尝试了
"Esimowl\\Mobimatter\\": "src/"
当我运行
composer dumpautoload -o
时,我收到以下错误
Class Esimowl\Mobimatter\Product located in ./src/Esimowl/Mobimatter/Product.php does not comply with psr-4 autoloading standard. Skipping.
我可能做错了什么?
顺便说一下
Product.php
就只有以下内容
<?php
namespace Esimowl\Mobimatter;
class Product {
public function __construct() {
if (is_admin()) {
echo 'Tshi is admin';
}
}
}
和
esimowl-mobimatter.php
刚刚有
<?php
/**
* Plugin Name: Esimowl MobiMatter Integration
* Plugin URI: https://jameshwartlopez.com
* Description: Toolkit for integrating to Mobimatter api
* Version: 1.0.0
* Author: Jameshwart
* Author URI: https://jameshwartlopez.com
* Text Domain: esimowl
* Requires at least: 6.2
* Requires PHP: 7.3
*
* @package Esimowl
*/
defined('ABSPATH') || exit;
define('ESIMOW_MOBIMATTER_PLUGIN_DIR', plugin_dir_path(__FILE__));
require ESIMOW_MOBIMATTER_PLUGIN_DIR . 'vendor/autoload.php';
根据文档,
psr-4
值:
[定义]从名称空间到路径的映射,相对于包根
这意味着键是基本命名空间,值是应该找到该命名空间的路径;该路径中的任何子目录都定义该名称空间的子包。因此,您的
Product
类的“规范”命名空间实际上是:
Esimowl\Mobimatter\Esimowl\Mobimatter\Product
现在,显然这很荒谬,因此您可以更改配置以匹配磁盘上的内容,将规范更改为以下之一:
"Esimowl\\Mobimatter\\": "src/Esimowl/Mobimatter"
"Esimowl\\": "src/Esimowl"
或者简单地说:
"": "src"
最后一个选项在同一文档中特别指出:
将在其中查找任何名称空间的后备目录
否则,您可以更改目录结构,使
Product.php
位于:
./src/Product.php
符合您原来的 PSR-4 规格。