批改状态:合格
老师批语:
- 实例演示通过空间引用类的三种方式;
- 类的别名引入与命名冲突的解决方案是什么?
- 写一个自动加载类;
- 将课堂提到的数据库操作命令全部上机做一遍,选择一些你认为有意思 很重要的发布到博客作业中…
<?php// 父空间namespace you1 {class Name{}// 1. 非限定名称: Name, 相当于“当前路径”// 类名前无“空间前缘”echo Name::class , '<br>';// 2. 限定名称: you2\Name,相当于“相对路径”// 类名前存在“非全局开始的空间前缀”echo you2\Name::class, '<br>';// 3. 完全限定名称: \deom\Name, 相当于“绝对路径”// 类名前存在“全局开始的空间前缀”echo \deom\Name::class;}// 子空间namespace you1\you2 {class Name{}}// 其它空间namespace deom {class Name{}}
图示:
<?php// 空间别名// 使用别名的原因有二个: 简化, 重名// 在这引用另一个空间成员:app\admin\models\MailModel.phpnamespace app\controller;// 引入类文件require 'index1-1.php';// 使用完全限定名称$user = new\app\admin\models\MailModel;var_dump($user);echo '<hr>';// 类名与当前空间的类重名了,就不能与原始类名相同,所以就必须写别名use app\admin\models\MailModel as Mail;// 别名访问$user = new Mail;var_dump($user);echo '<hr>';// 当前空间重名类class MailModel{}$user = new MailModel;var_dump($user);
引入文件:index1-1.php
<?phpnamespace app\admin\models;class MailModel{// ...}
图示:
自动加载器:loader.php
spl_autoload_register(function($class){$file = str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';require $file;});
文件:demo5.php
namespace app;use app\models\StaffsModel;use app\models\SalarysModel;// 引入自动加载器require 'app/loader.php';$Staffs = new StaffsModel;$Salarys = new SalarysModel;var_dump($Staffs,$Salarys);
app\models\StaffsModel.php
namespace app\models;class StaffsModel{}
app\models\SalarysModel.php
namespace app\models;class StaffsModel{}
图示:
-- 创建数据库 phpeducreate database phpedu collate utf8mb4_unicode_ci;-- 删除数据库 testdrop database test;-- 创建数据表 staffscreate table staffs(id int unsigned auto_increment not null primary key comment 'ID',name varchar(20) not null comment '姓名',gender enum('male', 'female') not null comment '性别',email varchar(120) not null comment '邮箱',birthday date not null comment '生日',create_at timestamp not null default current_timestamp comment '创建时间',update_at timestamp not null default current_timestamp on update current_timestamp comment '更新时间') engine = innodb auto_increment=1 collate=utf8mb4_unicode_ci;-- 删除数据表 testdrop table test-- 查看建表语句 staffsshow create table staffs-- 查看数据表show tables-- 查看表结构 staffsdesc staffs-- 添加表字段 salaryalter table staffs add salary int unsigned not null default 2000 after gender;-- 修改表字段 salaryalter table staffs change salary salary float not null default 1760 after gender;-- 删除表字段 testalter table staffs drop test;-- 表插入数据insert staffs (name, gender, salary, email, birthday) values('刘红军','male',8900,'lhj@sing.com','1989-06-01'),('李海涛','female',6600,'lht@sing.com','1998-10-22'),('张锋','female',9400,'zf@sing.com','1992-12-31'),('李家山','male',16800,'lhs@sing.com', '1998-8-5'),('赵二宝','female',13800,'zeb@sing.com', '1990-01-08'),('李明生','female',7300,'lms@sing.com','1989-09-18');-- 子查询插入(数据全复制插入)insert staffs (name, gender, salary, email, birthday) select name, gender, salary, email, birthday from staffs;
表插入数据图示:
子查询插入图示
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号