博客列表 >实例演示类的扩展,抽象,接口的语法 2. 全局成员有哪些,他们有哪些特点?为什么要用命名空间

实例演示类的扩展,抽象,接口的语法 2. 全局成员有哪些,他们有哪些特点?为什么要用命名空间

P粉314265155
P粉314265155原创
2022年08月22日 08:19:11292浏览

类与对象

  1. <?php
  2. // 类与对象
  3. namespace _0815;
  4. use _0815\Query as _0815Query;
  5. /**
  6. * 1. 属性重载: __get(),__set()
  7. * 2. 方法重载:__call($name,$args), __callStatic($name,$args)
  8. */
  9. // 封装, 重载, 继承
  10. class User {
  11. // 私有 封装
  12. private int $salary =10000;
  13. // 私有 封装
  14. private int $age=100;
  15. // 查询接口
  16. public function getAge(){
  17. return $this ->age;
  18. }
  19. }
  20. // 实例化
  21. $user = new User ();
  22. echo $user ->getAge() ;
  23. echo '<hr>';
  24. //--------------------------------------------
  25. class User2 {
  26. // 私有 封装
  27. private int $salary =10000;
  28. // 私有 封装
  29. private int $age=100;
  30. // 查询接口 属性拦截器 魔术方法 属性重载
  31. // 注意两个下划线
  32. public function __get($name){
  33. return $this ->$name;
  34. }
  35. }
  36. // 实例化
  37. $user2 = new User2 ();
  38. echo $user2 ->age ;
  39. echo '<hr>';
  40. //--------------------------------------------
  41. class User3 {
  42. // private 私有 封装 常用
  43. // $data 数据容器 用数组表示
  44. private array $data = [
  45. 'age' => 20,
  46. ];
  47. // 查询接口 属性拦截器 魔术方法 属性重载
  48. // 注意两个下划线
  49. // 商业代码
  50. public function __get($name){
  51. if (array_key_exists($name,$this ->data) ){
  52. return $this -> data[$name];
  53. }else {
  54. return "$name 不存在" ;
  55. }
  56. }
  57. }
  58. // 实例化
  59. $user3 = new User3 ();
  60. echo $user3 ->age ;
  61. echo '<hr>';
  62. echo $user3 ->dog ;
  63. echo '<hr>';
  64. //--------------------------------------------
  65. class User4 {
  66. // private 私有 封装 常用
  67. // $data 数据容器 用数组表示
  68. private array $data = [
  69. 'age' => 20,
  70. ];
  71. // 查询接口 属性拦截器 魔术方法 属性重载
  72. // 注意两个下划线
  73. // 商业代码
  74. public function __get($name){
  75. // 借助中间变量 result 实现单 return 返回
  76. $result =null ;
  77. if (array_key_exists($name,$this ->data) ){
  78. $result = $this -> data[$name];
  79. }else {
  80. $result = "$name 不存在" ;
  81. }
  82. return $result;
  83. }
  84. }
  85. // 实例化
  86. $user4 = new User4 ();
  87. echo $user4 ->age ;
  88. echo '<hr>';
  89. echo $user4 ->dog ;
  90. echo '<hr>';
  91. //--------------------------------------------
  92. class User5 {
  93. // private 私有 封装 常用
  94. // $data 数据容器 用数组表示
  95. private array $data = [
  96. 'age' => 20,
  97. ];
  98. // 查询接口 属性拦截器 魔术方法 属性重载
  99. // 注意两个下划线
  100. // 商业代码
  101. public function __get($name){
  102. // return array_key_exists($name ,$this ->data)? $this ->data[$name]:"$name 属性不存在";
  103. // 判断属性是否存在
  104. $condition = array_key_exists($name ,$this ->data);
  105. return $condition ? $this ->data[$name]:"$name 属性不存在";
  106. }
  107. // __set($name,$value): 见 812代码
  108. }
  109. // 实例化
  110. $user5 = new User5 ();
  111. echo $user5 ->age ;
  112. echo '<hr>';
  113. echo $user5 ->dog ;
  114. echo '<hr>';
  115. //--------------------------------------------
  116. class User6 {
  117. // private 私有 封装 常用
  118. // $data 数据容器 用数组表示
  119. private array $data = [
  120. 'age' => 20,
  121. ];
  122. // 查询接口 属性拦截器 魔术方法 属性重载
  123. // 注意两个下划线
  124. // 商业代码
  125. public function __get($name){
  126. // return array_key_exists($name ,$this ->data)? $this ->data[$name]:"$name 属性不存在";
  127. $condition = array_key_exists($name ,$this ->data);
  128. return $condition ? $this ->data[$name]:"$name 属性不存在";
  129. }
  130. // __set($name,$value): 见 812代码
  131. // 可以自动拦截对于方法的非法访问
  132. // 第一个参数是方法名称
  133. // 第二个参数是以数组表示的参数列表
  134. public function __call($name, $arguments)
  135. {
  136. // return '方法不存在';
  137. printf('方法名:%s<br>参数列表:<pre>%s</pre><br>',$name ,print_r($arguments,true));
  138. }
  139. public static function __callStatic($name, $arguments)
  140. {
  141. // 第一个参数是方法名称
  142. // 第二个参数是以数组表示的参数列表
  143. printf('方法名: %s<br>参数列表: <pre>%s</pre>', $name, print_r($arguments, true));
  144. }
  145. }
  146. // 实例化
  147. $user6= new User6 ();
  148. echo $user6->age ;
  149. echo '<hr>';
  150. echo $user6 -> cat;
  151. echo '<hr>';
  152. // echo $user6 -> hello();
  153. // echo '<hr>';
  154. // 非静态方法 类的实例访问
  155. echo $user6 -> hello('小狗','123@qq.om');
  156. echo '<hr>';
  157. // 静态方法 类访问 ::方法名
  158. User6 ::word (100,200);
  159. echo '<hr>';
  160. // tinkphp 查询构造器 数据库操作
  161. // Query 固定语法
  162. class Query {
  163. // 当这个查询类 实例化时,自动连接数据库 实例化的时候 自动连接数据库
  164. // public function __construct()
  165. // {
  166. // self::connect();
  167. // }
  168. public function table(){
  169. return $this;
  170. }
  171. public function where(){
  172. return $this;
  173. }
  174. public function find(){
  175. echo '完成';
  176. }
  177. }
  178. // 查询入口类
  179. class Db{
  180. public static function __callStatic($name, $arguments)
  181. {
  182. // echo $name ,',', $arguments [0];
  183. // 所有查询操作再次完成, 单入口查询,惰性查询
  184. return call_user_func_array( [ new Query,$name],$arguments );
  185. }
  186. }
  187. Db ::table( 'think_user' )->where('id', 1)->find();
  188. // 表名
  189. // Db::table('think_user')->where('id', 1)->find();

类的扩展

  1. <?php
  2. namespace _0815;
  3. use _0815\Demo1 as _0815Demo1;
  4. /**
  5. * 类的扩展/抽象/最终
  6. * 1. 可继承成员: protected
  7. * 2. extends: 类成员的来源
  8. * 3. parent: 父类引用
  9. */
  10. class Person
  11. {
  12. // 属性
  13. // 默认 public 公告成员,当前类 类外部
  14. // public $email = '123@qq.com';
  15. // private 私有成员 类外部不可见
  16. private int $id =18;
  17. // protected 受保护的
  18. protected string $name;
  19. // public > protected > private
  20. // 类中,类外,子类: public
  21. // 类中,子类: protected
  22. // 类中: private
  23. // 方法
  24. public function __construct($name)
  25. {
  26. $this ->name =$name;
  27. }
  28. // 自定义方法
  29. protected function getInfo(): string
  30. {
  31. return $this->name;
  32. }
  33. }
  34. // 继承 /扩展
  35. class Stu extends Person
  36. {
  37. // 只需要 扩展父类的属性和方法
  38. // 1、 属性扩展 扩展之前需要创建一个构造方法 继承扩展 相当于把父类除了 私有属性,其他都拿到了
  39. // extends: 相当于把父类代码复制到当前类中(除Private成员外)
  40. // protected string $name;
  41. // public function __construct($name)
  42. // {
  43. // $this->name = $name;
  44. // }
  45. // protected function getInfo(): string
  46. // {
  47. // return $this->name;
  48. // }
  49. // 属性扩展
  50. // 之前的
  51. // protected string $name;
  52. // 扩展的
  53. private string $lesson;
  54. private int $score;
  55. // 2、方法扩展
  56. // 构造方法
  57. public function __construct($name, $lesson, $score)
  58. {
  59. // $this ->nam = $name;
  60. // $this ->lesson = $lesson;
  61. // $this ->score = $score;
  62. // 简化 parent :引用父类 相当于执行了 父类的构造方法 获取所有能获取到的(除了私有
  63. parent::__construct($name);
  64. // $this ->name = $name;
  65. $this->lesson = $lesson;
  66. $this->score = $score;
  67. }
  68. // 自定义方法 拓展 注意 protected 要跟父类一样或者高于父 类 public
  69. public function getInfo(): string
  70. {
  71. return parent::getInfo() . '同学, (' . $this->lesson . ' ) , 成绩: ' . $this->score .'<br>';
  72. }
  73. }
  74. $stu = new Stu('小张', 'php', 90);
  75. echo $stu->getInfo() ;
  76. echo '<hr>';
  77. // 2. 禁用父类, 仅允许通过它的子类来访问父类成员
  78. // 把当前类,声明为"抽象类": abstract
  79. // 如果类中有抽象方法,则这个类必须声明为抽象类
  80. abstract class Demo1 {
  81. public string $name = 'admin';
  82. // 没有具体实现 叫抽象方法 前面必须加 abstract
  83. // 如果某个方法没有具体实现,应该声明成抽象方法
  84. abstract public static function getInfo($name);
  85. }
  86. class Demo2 extends Demo1 {
  87. // 在子类中, 必须将父类中的抽象方法实现了
  88. // public function getInfo($name)
  89. // {
  90. // return 'Hello, ' .$name;
  91. // }
  92. public static function getInfo($name)
  93. {
  94. return 'Hello, ' .$name;
  95. }
  96. }
  97. // new Demo2();
  98. echo Demo2::getInfo('朱老师');
  99. echo '<hr>';
  100. // 抽象类: 强制要求必须继承才能用
  101. // 最终类: 禁止继承,直接用
  102. // final: 不能扩展了
  103. // final class Demo3
  104. // {
  105. // }
  106. // class Demo4 extends Demo3
  107. // {
  108. // }

接口

  1. <?php
  2. namespace _0815;
  3. use _0815\Demo2 as _0815Demo2;
  4. // 接口 升级版的抽象类
  5. // abstract class = interface 接口
  6. // 接口类
  7. interface iUser{
  8. // public string $name;
  9. // // 如果 某个方法 没有具体实现,应该声明为抽象方法
  10. // abstract public static function getInfo ($name);
  11. // abstract public static function getInfo1 ($score);
  12. // abstract public static function getInfo2 ($age);
  13. // 接口里面两类成员 常量、方法
  14. // 常量
  15. public const NATION = 'CHINA';
  16. // 方法 public
  17. public function m1();
  18. public function m2();
  19. }
  20. //工作类 实现了 接口的类 将接口类方法实现
  21. // class Demo1 implements iUser{
  22. // // 接口的抽象方法,必须在工作类中全部实现
  23. // public function m1(){
  24. // }
  25. // public function m2(){
  26. // }
  27. // }
  28. // 如果实现类 仅实现了接口 一部分抽象方法 应该为抽象类
  29. // iUser 接口类固定写法
  30. abstract class Demo2 implements iUser{
  31. // 部分实现时,需要将类变为抽象类
  32. public function m1(){
  33. }
  34. }
  35. class Demo3 extends Demo2{
  36. public function m2(){
  37. }
  38. }
  39. // php是单继承 ,但是可以 使用接口多继承
  40. interface A {
  41. }
  42. interface B {
  43. }
  44. interface C {
  45. }
  46. // Test类,同时从三个接口中获取成员,间接实现了"多继承"
  47. // 用途:对于增删改查
  48. class Test implements A,B,C {
  49. }
  50. interface iDb {
  51. // 插入
  52. public static function insert (array $data);
  53. // 更新
  54. public static function updata (array $data ,string $where );
  55. // 删除
  56. public static function delete (string $where );
  57. // 查询
  58. public static function select (array $option );
  59. }
  60. abstract class aDb {
  61. // 插入
  62. public static function insert (array $data){}
  63. // 更新
  64. public static function updata (array $data ,string $where ){}
  65. // 删除
  66. public static function delete (string $where ){}
  67. // 查询
  68. public static function select (array $option ){}
  69. }
  70. // 实现
  71. class Db extends aDb {
  72. }

全局成员

  1. <?php
  2. namespace _0815;
  3. // 全局成员:函数 常量、类、接口、全局有效,禁止重复声明
  4. function hello(){
  5. }
  6. const A =1 ;
  7. class B {
  8. }
  9. // 将第二个 hello 声明在另外一个空间中,不会重名了
  10. namespace _0815_1;
  11. function hello (){
  12. }
  13. const A =2;
  14. class B {
  15. }

命名空间

  1. <?php
  2. // 命名空间解决了:全局成员的命名冲突,借鉴了同名文件可存入在不同的目录下面
  3. namespace one;
  4. class Demo1 {
  5. public static string $name = '小猫';
  6. }
  7. // 当存在 命名空间时,全局成员应该使用完整的名称
  8. // 类名 = 空间名称\类名
  9. // ::class 获取 类的完整名称
  10. echo Demo1::class.'<br>';
  11. echo '<hr>';
  12. namespace two;
  13. class Demo1 {
  14. public static string $name = '小狗';
  15. }
  16. echo Demo1::class.'<br>';
  17. echo '<hr>';
  18. // 跨空间访问
  19. // 在two 里面访问 one
  20. // 报错的 实际访问路径变成了 two\one\Demo1
  21. // echo one\Demo1::$name;
  22. // 跨空间访问 正确写法 从根空间/全局空间写起 开始查询 用反斜线 \
  23. echo \one\Demo1::$name;
  24. echo '<hr>';
  25. echo \two\Demo1::$name;

命名空间类型

  1. <?php
  2. // 命名空间类型、
  3. // 1. 当前路径: 非限定名称, Index Index::show();
  4. // 2. 相对路径: 限定名称, two\Index
  5. // 3. 绝对路径: 完全限定名称, \one\two\Index
  6. namespace _0815;
  7. namespace one;
  8. class Index {
  9. public static function show(){
  10. return __METHOD__;
  11. }
  12. }
  13. // Index::show(); 非限定名称
  14. // one\Index::show
  15. echo Index::show();
  16. echo '<hr>';
  17. // 如果我在one空间中, 访问 one\two中的成员
  18. // 方式一 绝对路径 完全限定名称
  19. echo \one\two\Index::show(). '<br>';
  20. // 方式二 限定名称
  21. echo two\Index::show(). '<br>';
  22. // 空间可以分层/分级管理
  23. namespace one\two;
  24. class Index {
  25. public static function show(){
  26. return __METHOD__;
  27. }
  28. }
  29. // one\two\Index::show
  30. echo Index::show();
  31. echo '<hr>';

use 简化命令空间/类名

  1. <?php
  2. namespace _0815;
  3. namespace one;
  4. class Index {
  5. public static function show(){
  6. return __METHOD__;
  7. }
  8. }
  9. echo two\three\Index::show();
  10. echo '<hr>';
  11. // use 默认使用完全限定名称的类名/绝对路径
  12. // echo \one\two\three\Index::show();
  13. // use 进行简化
  14. use \one\two\three\Index as UserIndex;
  15. echo UserIndex::show();
  16. echo '<hr>';
  17. namespace one\two\three;
  18. class Index {
  19. public static function show(){
  20. return __METHOD__;
  21. }
  22. }
  23. //---------------------------------------------
  24. namespace four;
  25. // 如果当前空间中的没有Index类,可以进一步的简化
  26. // use one\two\three\Index as Index;
  27. // 如果当前类别名与原始类名相同,可以不写
  28. // 如果当前空间中有Index类, 就不能省去as
  29. use one\two\three\Index;
  30. echo Index::show() ;
  31. echo '<hr>';
  32. namespace five;
  33. class Index {
  34. public static function show(){
  35. return __METHOD__;
  36. }
  37. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议