Blogger Information
Blog 47
fans 3
comment 0
visits 38226
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
命名空间类三种引用、类导入命名冲突解决、自动加载类、mysql操作
Original
840 people have browsed it

1.空间引用类的三种方式

  1. // 父空间
  2. namespace test {
  3. class name
  4. {
  5. }
  6. // 1. 非限定名称:name,相当于当前路径
  7. // 类名前无空间前缀
  8. echo name::class,'<br>';
  9. // 2.限定名称:test2\name,相当于相对路径
  10. // 类名前存在非全局开始的空间前缀
  11. echo test2\name::class,'<br>';
  12. // 3.完全限定名称:\two\name, 相当于绝对路径
  13. // 类名前存在全局开始的空间前缀
  14. echo \two\name::class,'<br>';
  15. }
  16. // 子空间
  17. namespace test\test2 {
  18. class name
  19. {
  20. }
  21. }
  22. // 其他空间
  23. namespace two {
  24. class name
  25. {
  26. }
  27. }

2. 类的别名引入与命名冲突的解决方案

使用空间别名的原因:简化、重名

  • 文件:demo3.php
  1. namespace app\controller;
  2. // 引入类文件
  3. require 'demo3-1.php';
  4. // 使用完全限定名称
  5. $user = new \app\admin\models\SalaryModel;
  6. var_dump($user);
  7. echo '<hr>';
  8. // 类名与当前空间的类重名了,就不能与原始类名相同,所以就必须写别名
  9. use app\admin\models\SalaryModel as Salary;
  10. // 别名访问
  11. $user = new Salary;
  12. var_dump($user);
  13. echo '<hr>';
  14. // 当前空间重名类
  15. class SalaryModel
  16. {
  17. }
  18. $user = new SalaryModel;
  19. var_dump($user);
  • 引入文件:demo3-1.php
  1. namespace app\admin\models;
  2. class SalaryModel
  3. {
  4. }

3. 自动加载类

  • 自动加载器:loader.php
  1. spl_autoload_register(function($class){
  2. $file = str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
  3. require $file;
  4. });
  • 文件:demo5.php
  1. namespace app;
  2. use app\models\StaffsModel;
  3. use app\models\SalarysModel;
  4. // 引入自动加载器
  5. require 'app/loader.php';
  6. $Staffs = new StaffsModel;
  7. $Salarys = new SalarysModel;
  8. var_dump($Staffs,$Salarys);
  • app\models\StaffsModel.php
  1. namespace app\models;
  2. class StaffsModel
  3. {
  4. }
  • app\models\SalarysModel.php
  1. namespace app\models;
  2. class StaffsModel
  3. {
  4. }

4.mysql命令

  1. -- 创建数据表
  2. create database phpedu collate utf8mb4_unicode_ci;
  3. -- 选择默认数据库
  4. use phpedu;
  5. -- 删除数据表
  6. drop database user;
  7. -- 创建数据表users
  8. create table users (
  9. sid int unsigned auto_increment not null primary key,
  10. name varchar(20) not null comment '姓名',
  11. gender enum('male','female') not null comment '性别',
  12. email varchar(150) not null comment '邮箱',
  13. birthday date not null comment '生日',
  14. create_at timestamp not null default current_timestamp comment '创建日期',
  15. update_at timestamp not null default current_timestamp on update current_timestamp comment '更新日期'
  16. ) engine = innodb auto_increment = 1 collate = utf8mb4_unicode_ci;
  17. -- 增加字段
  18. alter table users add salary int unsigned not null default 2999 after gender;
  19. -- 更新字段定义
  20. alter table users change salary salary float unsigned not null default 3000 after gender;
  21. -- 删除字段
  22. alter table users drop test;
  23. -- 删除表
  24. drop table test;
  25. -- 插入 insert
  26. insert users (name,gender,salary,email,birthday)
  27. values ('joe','male',3999,'joe@qq.com','1988-09-02');
  28. -- 批量插入
  29. insert users (name,gender,salary,email,birthday) values
  30. ('hack','male',5566,'hack@qq.com','2000-02-23'),
  31. ('jack','male',4533,'jack@qq.com','1966-02-23'),
  32. ('mack','male',2566,'mack@qq.com','1997-02-23'),
  33. ('unik','male',6555,'unik@qq.com','1987-02-23'),
  34. ('ming','male',1999,'hack@qq.com','1990-02-23');
  35. -- 子查询式插入,复制插入
  36. insert users (name,gender,salary,email,birthday)
  37. (select name,gender,salary,email,birthday from users);
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments