I have the following file structure
- esimowl-mobimatter
- - src
- - - Esimowl
- - - - Mobimatter
- - - - - Product.php
- - composer.json
- - esimowl-mobimatter.php
my composer.json file have the following content
{
"name": "esimowl/mobimatter",
"description": "An awesome plugin created by Esimowl",
"type": "wordpress-plugin",
"autoload": {
"psr-4": {
"Esimowl\\Mobimatter\\": "./src/"
}
}
}
Please note i have tried "Esimowl\\Mobimatter\\": "src/" in my composer.json
When I run composer dumpautoload -o i got the following error
Class Esimowl\Mobimatter\Product located in ./src/Esimowl/Mobimatter/Product.php does not comply with psr-4 autoloading standard. Skipping.
What could Have I done wrong?
By the way Product.php just have the following content
<?php
namespace Esimowl\Mobimatter;
class Product {
public function __construct() {
if (is_admin()) {
echo 'Tshi is admin';
}
}
}
and esimowl-mobimatter.php just have
<?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';
As per the documentation, the
psr-4value:What this means is that the key is the base namespace, and the value the path where that namespace should be found; any sub-directories in that path define sub-packages of that namespace. So, the "canonical" namespace for your
Productclass would actually be:Now, obviously that's ridiculous, so you can change your configuration to match what you have on disk, changing the specification to one of:
or simply:
This last option is specifically called out in the same documentation as:
Otherwise you can change the directory structure such that
Product.phpis located at:to match your original PSR-4 specification.