0

I know many similar questions have been asked but I have followed every guide I can find and it still is not working for me. I don't know if it has to do with it being a WordPress plugin, but the autoloader is not finding my classes.

In my plugin, I am running into a lot of Class Name collisions. I know I could prefix every class with the name of the plugin but I feel like it would be easier/cleaner to have the entire project under a specific namespace. I am still somewhat new to PHP so I may be thinking about this all wrong but I think it should work. I understand there are other ways to make this work but I would like to understand why this isn't working. Thank you for any input or help you can offer!

The error I am getting:

Fatal error: Class 'DistinguishedSites\Inc\Base\Activate' not found in /home/pluginte/public_html/wp-content/plugins/distinguished-sites/distinguished-sites-plugin.php on line 47 

Simplified directory structure:

/distinguished-sites - distinguished-sites-plugin.php - composer.json - /vendor - *All vendor files* - /inc - /Base - Activate.php *etc...* 

Activate.php file:

<?php /** * @package DistinguishedSites */ namespace DistinguishedSites\Inc\Base; class Activate { public static function activate() { $db = new DatabaseApi(); $filter = new PostFilter; $db->createTable(); $results = $filter->filterPosts(); flush_rewrite_rules(); } } 

I am trying to use the Activate class in my distinguished-sites-plugin.php file:

<?php /** * @package DistinguishedSites */ use \DistinguishedSites\Inc\Base\Activate; defined( 'ABSPATH' ) or die( 'Hey, what are you doing here? You silly human!' ); // Require autoload file. if ( file_exists( dirname( __FILE__ ) . '/vendor/autoload.php' ) ) { require_once dirname( __FILE__ ) . '/vendor/autoload.php'; } define( 'PLUGIN_PATH', plugin_dir_path( __FILE__ )); define( 'PLUGIN_URL', plugin_dir_url( __FILE__ )); define( 'PLUGIN', plugin_basename( __FILE__ )); // Runs on plugin activation. function activate_distinguished_sites_plugin() { Activate::activate(); } register_activation_hook( __FILE__, 'activate_distinguished_sites_plugin'); // Runs of plugin deactivation. function deactivate_distinguished_sites_plugin() { //Deactivate::deactivate(); } register_deactivation_hook( __FILE__, 'deactivate_distinguished_sites_plugin'); // Initialize core classes of the plugin. if ( class_exists( 'DistinguishedSites\Init' ) ) { Init::register_services(); } 

composer.json file:

{ "name": "sno/distinguished-sites", "description": "Distinguished sites badge submission plugin.", "type": "project", "license": "GPL", "authors": [ { "name": "noah", "email": "[email protected]" } ], "minimum-stability": "dev", "require": {}, "autoload": { "psr-4": { "DistinguishedSites\\": "." } } } 

autoload_psr4.php file:

<?php // autoload_psr4.php @generated by Composer $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( 'DistinguishedSites\\' => array($baseDir . '/'), ); 

1 Answer 1

2

PSR-4 autoloader is case sensitive, so you should either capitalize your Inc/ directory, or use lowercase in your namespaces.

The subdirectory name MUST match the case of the sub-namespace names.

https://www.php-fig.org/psr/psr-4/

Sign up to request clarification or add additional context in comments.

2 Comments

Wow... the project I used for reference had 'Inc\\' => array($baseDir . '/inc') in the psr-4 file. I didn't realize that was making up for the case difference. Thank you so much!
Thanks for this answer, I was banging my head on the wall trying to figure out why my plugin was working locally but not on a live server. FYI to anyone seeing this. If you're committing to git it will lowercase your folder names. You need to tell git that it should not ignore the cases of folder names by running git config core.ignorecase false in your terminal.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.