Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
实例演示通过空间引用类的三种方式;
<?php
namespace na1 {
class User
{
}
echo User::class,'<br>';
echo na2\User::class,'<br>';
echo \sss\User::class,'<br>';
}
namespace na1\na2 {
class User
{
}
}
namespace sss {
class User
{
}
}
2.类的别名引入与命名冲突的解决方案是什么?
<?php
// 这是admin.php文件夹
namespace admin\acc {
class User
{
}
}
<?php
namespace acc {
class User
{
}
require 'admin.php';
use admin\acc\User as adminuser;
$user = new adminUser;
$user1 = new User;
var_dump($user);
echo '<br>';
var_dump($user1);
}
3.写一个自动加载类;
<?php
//文件地址:app\models\Ass.php
namespace app\models;
class Ass
{
}
<?php
//文件地址:app\models\User.php
namespace app\models;
class User
{
}
<?php
//自动加载文件
spl_autoload_register(function ($class){
$file= str_replace('\\',DIRECTORY_SEPARATOR,$class).'.php';
require $file;
});
<?php
namespace app;
require 'app/loader.php';
use app\models\Ass;
use app\models\User;
$ass=new Ass;
$user=new User;
var_dump($ass, $user);