// Define the autoloader function function myAutoloader($className) { $path = __DIR__ . '/' . str_replace('\\', '/', $className) . '.php'; if (file_exists($path)) { require_once $path; } } // Register the autoloader function spl_autoload_register('myAutoloader'); // Use the class $myClass = new MyClass();
// Define the autoloader function function myAutoloader($className) { require_once __DIR__ . '/vendor/autoload.php'; } // Register the autoloader function spl_autoload_register('myAutoloader'); // Use the class $myClass = new MyClass();In this example, we use Composer to manage our dependencies and autoload the required classes. We define an autoloader function that only requires the Composer autoloader file. Finally, we can create an instance of the class without explicitly loading the class file. Package library: Composer Autoloader