带命名空间的完整示例
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(); ?>
index.php
<?php
spl_autoload_register(function($class_name) {
@include_once(__DIR__ . '/' . strtolower(str_replace('\\', '/', $class_name)) . '.php');
});
new Vegetables\Carrot();
?>
结果
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'); spl_autoload_call('Fruits\\Pinapple'); new Vegetables\Carrot();
?>
结果
pinapple
carrot