I'm struggling with spl_autoloading
inside a namespaced WordPress Plugin.
I already revised these questions:
My folder structure looks like that:
Namespacing everything below SamplePlugin
inside wp-content/plugins/sample-plugin
src
'-- SamplePlugin
' '-- ShortCode.php
'-- autoload.php
sample-plugin.php
My current autoload function (as I'm used to doing it...)
spl_autoload_register(function ($class) {
$dir = 'src/';
$file = str_replace('\\', '/', $class) . '.php';
require_once $dir . $file;
Using that, the prefixing namespace is correctly added, so if I instantiate in sample-plugin.php
:
namespace SamplePlugin;
require_once 'src/autoload.php';
$shortcode = new ShortCode();
I get the following output (VVV):
Warning: require_once(src/SamplePlugin/ShortCode.php): failed to open stream: No such file or directory in /srv/www/wordpress-default/wp-content/plugins/sample-plugin/src/autoload.php on line 8
Fatal error: require_once(): Failed opening required 'src/SamplePlugin/ShortCode.php' (include_path='.:/usr/share/php:/usr/share/pear') in /srv/www/wordpress-default/wp-content/plugins/sample-plugin/src/autoload.php on line 8
Any hints?
All Classes (e.g. ShortCode are callable through SamplePlugin\Shortcode.