实例演示通过空间引用类的三种方式;
<?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);
- 将课堂提到的数据库操作命令全部上机做一遍,
- 1.创建数据库
create database html collate ; - 2.进入数据库并直接选择要修改的数据库
-mysql -u root -p root html; - 3.创建数据表
create table css (
id int unsigened auto_increment not null primary key,
name varchar(20) not null comment ‘姓名’,
)engine=innodb auto_increment=0 collate=utfomb4_unicode_ci; - 4.增加字段
alter table css add email varchat(150) not null after id , - 5.更新字段
alter table css change email email varchat(100) not null comment ‘邮箱’ after id,