批改状态:合格
老师批语:
<?php// 类名的三种引用方式: 解决类来自于哪一个空间?// 文件系统路径格式:// 绝对路径: c:\web\php\hello.php// 相对路径: ../img/dog.jpg// 当前路径: index.html// 父级namespace a{class a{}// 1. 非限定名称: a, 相当于“当前路径”// 类名前无“空间前缘”echo a::class , '<br>';// 2. 限定名称: a\b,相当于“相对路径”// 类名前存在“非全局开始的空间前缀”echo b\b::class, '<br>';// 3. 完全限定名称: \one\User, 相当于“绝对路径”// 类名前存在“全局开始的空间前缀”echo \c::class, '<br>';}// 子级namespace a\b{class b{}}// 全局空间namespace {class c{}}// 动态类的应用namespace d{class d{const ad =__DIR__;}// 动态类: 类名在变量中// 赋值时必须使用: 完全限定名称$a = '\d\d';echo $a::ad;}
<?php// 使用别名的原因有二个: 1.简化, 2.解决重名问题namespace a{use \user\user;require '1.php';echo user::class.'<hr>';// 使用别名简化use \user\user as users;// 使用别名访问$obj = new users;echo $obj::name.'<hr>';// 如果别名与原始的类名部分相同,别名UserModel,与原始类名UserModel同名,此时可以不写别名// 注:类别名如不能与原始类名相同,所以与不能省略了use \admin\admin\a;require 'admin/admin/a.php';$obj1 =new a;echo $obj1::id;// 当空间重名时可用别名}
autoload.php代码
<?phpspl_autoload_register(function($class){$file = str_replace('\\','/',$class).'.php';require $file;});
调用代码
<?phpuse admin\admin\a;use admin\admin\b;require 'admin/autoload.php';var_dump(new a,new b);
数据库操作笔记
-- 创建数据表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号