I think the problem here is that you’re using a relative path in autoload.php
inside the src/
subfolder,
Try for example to modify your src/autoload.php
to use absolute paths instead:
\spl_autoload_register( function( $class )
{
$dir = plugin_dir_path( __FILE__ );
$file = str_replace( '\\', "https://wordpress.stackexchange.com/", $class ) . '.php';
$path = $dir . $file;
if( file_exists( $path ) )
require_once $path;
} );
where we add a file_exists
to be plugin class specific.
Update
Here’s the main /sample-plugin/sample-plugin.php
file:
<?php
/**
* Plugin Name: Sample Plugin
*/
namespace SamplePlugin;
require_once __DIR__ . '/src/autoload.php';
// require_once 'src/autoload.php';
$shortcode = new ShortCode();
Here’s the /simple-plugin/src/SamplePlugin/Shortcode.php
file:
<?php
/**
* Class Shortcode
*/
namespace SamplePlugin;
class Shortcode
{
public function __construct()
{
print 'DEBUG: Greetings from the Shortcode instance!';
}
}
You could also check out how Composer generates the vendor/autoload.php
file, that’s included with:
require __DIR__ . '/vendor/autoload.php';