A complete example with namespaces:
fruits/pinapple.php
<?php
namespace Fruits;
echo "pinapple\n";
class Pinapple { }
?>
fruits/pinapple.php
<?php
namespace Vegetables;
use Fruits\Pinapple;
echo "carrot\n";
class Carrot { }
new Pinapple(); // Let's call autoload here
?>
index.php
<?php
spl_autoload_register(function($class_name) {
@include_once(__DIR__ . '/' . strtolower(str_replace('\\', '/', $class_name)) . '.php');
});
new Vegetables\Carrot();
?>
Result:
carrot
pinapple
index2.php
<?php
spl_autoload_register(function($class_name) {
@include_once(__DIR__ . '/' . strtolower(str_replace('\\', '/', $class_name)) . '.php');
});
spl_autoload_call('Fruits\\Pinapple'); // Reverse the load order
spl_autoload_call('Fruits\\Pinapple'); // Multiple call is safe with include_once
new Vegetables\Carrot();
?>
Result:
pinapple
carrot