Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
实现自动加载,应该遵循如下约定:
use
默认必须是”完全限定名称”,所以"\"
全局空间标识符可以不写$path = str_replace('\\', DIRECTORY_SEPARATOR, $class);
require __DIR__ . DIRECTORY_SEPARATOR. $class.'.php';
<?php
namespace index;
// 引入自动加载
require 'auto.php';
use inc\con1;
use inc\con2;
use inc\con3;
echo con1::index() . '<hr>';
echo con2::index() . '<hr>';
echo con3::index() . '<hr>';
<?php
// 自动加载
spl_autoload_register(function ($class) {
$dir = str_replace('\\', DIRECTORY_SEPARATOR, $class);
require __DIR__ . DIRECTORY_SEPARATOR . $dir . '.php';
});
<?php
namespace inc;
class con1
{
public static function index()
{
return __CLASS__ . '类<br>' . __METHOD__ . '方法';
}
}
<?php
namespace inc;
class con2
{
public static function index()
{
return __CLASS__ . '类<br>' . __METHOD__ . '方法';
}
}
<?php
namespace inc;
class con3
{
public static function index()
{
return __CLASS__ . '类<br>' . __METHOD__ . '方法';
}
}