博主信息
博文 37
粉丝 2
评论 0
访问量 34576
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
0223-空间引用类的三种方式,写一个自动加载类;
世纪天城
原创
871人浏览过

空间引用类的三种方式

  1. <?php
  2. // 类名的三种引用方式: 解决类来自于哪一个空间?
  3. // 文件系统路径格式:
  4. // 绝对路径: c:\web\php\hello.php
  5. // 相对路径: ../img/dog.jpg
  6. // 当前路径: index.html
  7. // 父级
  8. namespace a{
  9. class a
  10. {
  11. }
  12. // 1. 非限定名称: a, 相当于“当前路径”
  13. // 类名前无“空间前缘”
  14. echo a::class , '<br>';
  15. // 2. 限定名称: a\b,相当于“相对路径”
  16. // 类名前存在“非全局开始的空间前缀”
  17. echo b\b::class, '<br>';
  18. // 3. 完全限定名称: \one\User, 相当于“绝对路径”
  19. // 类名前存在“全局开始的空间前缀”
  20. echo \c::class, '<br>';
  21. }
  22. // 子级
  23. namespace a\b{
  24. class b
  25. {
  26. }
  27. }
  28. // 全局空间
  29. namespace {
  30. class c
  31. {
  32. }
  33. }
  34. // 动态类的应用
  35. namespace d{
  36. class d
  37. {
  38. const ad =__DIR__;
  39. }
  40. // 动态类: 类名在变量中
  41. // 赋值时必须使用: 完全限定名称
  42. $a = '\d\d';
  43. echo $a::ad;
  44. }

类的别名引入与

  1. <?php
  2. // 使用别名的原因有二个: 1.简化, 2.解决重名问题
  3. namespace a{
  4. use \user\user;
  5. require '1.php';
  6. echo user::class.'<hr>';
  7. // 使用别名简化
  8. use \user\user as users;
  9. // 使用别名访问
  10. $obj = new users;
  11. echo $obj::name.'<hr>';
  12. // 如果别名与原始的类名部分相同,别名UserModel,与原始类名UserModel同名,此时可以不写别名
  13. // 注:类别名如不能与原始类名相同,所以与不能省略了
  14. use \admin\admin\a;
  15. require 'admin/admin/a.php';
  16. $obj1 =new a;
  17. echo $obj1::id;
  18. // 当空间重名时可用别名
  19. }

自动加载类

autoload.php代码

  1. <?php
  2. spl_autoload_register(function($class){
  3. $file = str_replace('\\','/',$class).'.php';
  4. require $file;
  5. });

调用代码

  1. <?php
  2. use admin\admin\a;
  3. use admin\admin\b;
  4. require 'admin/autoload.php';
  5. var_dump(new a,new b);

数据库操作笔记

  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);
批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学