批改状态:合格
老师批语:
<?php// php默认是单继承// 项目不大, 不想用接口实现多继承// 又不想用父类,子类这样的继承方式来增加项目复杂度// 可以试这个"迷你"版的基类, trait// trait 可以看成是一个通用组件库,// trait 插入到任何一个类中, 来扩展这个类的功能// insteadOf 用在 trait 中解决 命名冲突// trait 一个特殊的类namespace _0816;// 1、经典单继承class A {public static function hello(){return __METHOD__.'我是A类方法'.'<hr>';}}class B extends A {}echo B::hello();// B的类名echo B::class;echo '<hr>';echo call_user_func([B::class,'hello']);echo '<hr>';// 2、接口简介实现了多继承interface iA {public static function hello ();}interface iB {public static function world();}class W implements iA, iB{public static function hello(){return __METHOD__.'我是w里面的A的方法';}public static function world(){return __METHOD__.'我是w里面的B的方法';}}echo call_user_func([W::class,'hello' ]).'<hr>';echo call_user_func([W::class,'world' ]).'<hr>';// 3、traittrait tA{public static function sex(){return __METHOD__.'我是TA的sex方法';}}trait tB{public static function email(){return __METHOD__.'我是TB的sex方法';}}class tC {use tA;use tB;}echo call_user_func([tC::class,'sex' ]).'<hr>';echo call_user_func([tC::class,'email' ]).'<hr>';echo'<hr>';// trait 的优先级// 父类parent < trait <子类class tG {public static function hello(){return __METHOD__.'我是 tD的hello方法';}}trait tJ{public static function hello(){return __METHOD__.'我是TJ的hello方法';}}class tH extends tG {use tJ;public static function hello(){return __METHOD__.'我是 TH 的hello方法';}use tJ;}echo call_user_func([tH::class,'hello' ]).'<hr>';echo '<hr>';// // 5. trait 命名冲突trait t2 {public static function hello(){return __METHOD__.'我是t2的hello方法';}}trait t1 {public static function hello(){return __METHOD__.'我是t1的hello方法';}}trait W3 {use t2,t1 {t2::hello as t3;t1::hello insteadOf t2;}}echo call_user_func([W3::class,'hello' ]).'<hr>';echo '<hr>';// 6. trait 优化trait t3 {public static function hello(){return __METHOD__.'我是t3的hello方法';}}trait t4 {public static function world(){return __METHOD__.'我是t4的world方法';}}trait t5{public static function sex(){return __METHOD__.'我是t5的 sex方法';}}trait t6 {public static function age(){return __METHOD__.'我是t6的age方法';}}// 简化trait importALL {use t3;use t4;use t5,t6;}trait W4 {// use t3;// use t4;// use t5,t6;use importALL ;}echo call_user_func([W4::class,'hello' ]).'<hr>';echo call_user_func([W4::class,'world' ]).'<hr>';echo call_user_func([W4::class,'sex' ]).'<hr>';echo call_user_func([W4::class,'age' ]).'<hr>';echo '<hr>';
<?php// 自动加载器// namespace thinkPHP ;namespace admin;// 查看命名空间名称echo __NAMESPACE__;echo '<hr>';// 当前空间的引用// echo namespace;class A {}// new A();或者 namesapce ==tinkPHP 当前空间的引用new namespace\A();// 访问三个控制器 Demo1 Demo2 Demo 3 的 hello 方法// 加载 Demo1// require __DIR__.'/admin/controller/Demo1.php';// // 加载 Demo2// require __DIR__.'/admin/controller/Demo2.php';// // 加载 Demo3// require __DIR__.'/admin/controller/Demo3.php';//----------------------------------------------------------// 自动加载器// 为了实现自动加载,应该遵循一些约定// 1. 一个文件只有一个类// 2. 这个类名和文件名必须一致// 3. 这个类的命名空间,必须映射到类文件所在的路径// 函数注册// spl_autoload_register(function ($class) {// echo $class.'<hr>';// $path = str_replace('\\',DIRECTORY_SEPARATOR,$class);// // 增加判断 文件是否存在// $file =$path .'.php';// if(file_exists( $file)){// require $path . '.php';// }else{// echo '文件不存在';// }// });// 输出 Demo1 方法 完全限定名称:绝对路径// echo \admin\controller\Demo1::hello().'<hr>';// 限定名称:相对路径 命名空间名称变为admin 前面的admin可以省略echo controller\Demo1::hello().'<hr>';// 进一步简化// 别名 :从完全限定名称写起// use// 非限定名称 默认从全局开始 admin前面的 \ 可以省略 全局空间标识符可以不写use \admin\controller\Demo2;echo Demo2::hello();// 输出 Demo2 方法// echo \admin\controller\Demo2::hello().'<hr>';// 输出 Demo3 方法// echo \admin\controller\Demo3::hello().'<hr>';
<?phpnamespace admin;require 'autoloader.php';use admin\controller\Demo1;echo Demo1::hello();
<?php// 为了实现自动加载,应该遵循一些约定// 1. 一个文件只有一个类// 2. 这个类名和文件名必须一致// 3. 这个类的命名空间,必须映射到类文件所在的路径namespace admin\controller;class Demo1 {public static function hello(){return __METHOD__;}}
<?php// 为了实现自动加载,应该遵循一些约定// 1. 一个文件只有一个类// 2. 这个类名和文件名必须一致// 3. 这个类的命名空间,必须映射到类文件所在的路径namespace admin\controller;class Demo2 {public static function hello(){return __METHOD__;}}
<?php// 为了实现自动加载,应该遵循一些约定// 1. 一个文件只有一个类// 2. 这个类名和文件名必须一致// 3. 这个类的命名空间,必须映射到类文件所在的路径namespace admin\controller;class Demo3 {public static function hello(){return __METHOD__;}}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号