批改状态:合格
老师批语:

// 父空间namespace test {class name{}// 1. 非限定名称:name,相当于当前路径// 类名前无空间前缀echo name::class,'<br>';// 2.限定名称:test2\name,相当于相对路径// 类名前存在非全局开始的空间前缀echo test2\name::class,'<br>';// 3.完全限定名称:\two\name, 相当于绝对路径// 类名前存在全局开始的空间前缀echo \two\name::class,'<br>';}// 子空间namespace test\test2 {class name{}}// 其他空间namespace two {class name{}}
使用空间别名的原因:简化、重名

namespace app\controller;// 引入类文件require 'demo3-1.php';// 使用完全限定名称$user = new \app\admin\models\SalaryModel;var_dump($user);echo '<hr>';// 类名与当前空间的类重名了,就不能与原始类名相同,所以就必须写别名use app\admin\models\SalaryModel as Salary;// 别名访问$user = new Salary;var_dump($user);echo '<hr>';// 当前空间重名类class SalaryModel{}$user = new SalaryModel;var_dump($user);
namespace app\admin\models;class SalaryModel{}

spl_autoload_register(function($class){$file = str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';require $file;});
namespace app;use app\models\StaffsModel;use app\models\SalarysModel;// 引入自动加载器require 'app/loader.php';$Staffs = new StaffsModel;$Salarys = new SalarysModel;var_dump($Staffs,$Salarys);
namespace app\models;class StaffsModel{}
namespace app\models;class StaffsModel{}

-- 创建数据表create database phpedu collate utf8mb4_unicode_ci;-- 选择默认数据库use phpedu;-- 删除数据表drop database user;-- 创建数据表userscreate table users (sid int unsigned auto_increment not null primary key,name varchar(20) not null comment '姓名',gender enum('male','female') not null comment '性别',email varchar(150) 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;-- 增加字段alter table users add salary int unsigned not null default 2999 after gender;-- 更新字段定义alter table users change salary salary float unsigned not null default 3000 after gender;-- 删除字段alter table users drop test;-- 删除表drop table test;-- 插入 insertinsert users (name,gender,salary,email,birthday)values ('joe','male',3999,'joe@qq.com','1988-09-02');-- 批量插入insert users (name,gender,salary,email,birthday) values('hack','male',5566,'hack@qq.com','2000-02-23'),('jack','male',4533,'jack@qq.com','1966-02-23'),('mack','male',2566,'mack@qq.com','1997-02-23'),('unik','male',6555,'unik@qq.com','1987-02-23'),('ming','male',1999,'hack@qq.com','1990-02-23');-- 子查询式插入,复制插入insert users (name,gender,salary,email,birthday)(select name,gender,salary,email,birthday from users);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号