Blogger Information
Blog 43
fans 4
comment 0
visits 18985
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP类的继承/抽象类的实现/命名空间
汇享科技
Original
403 people have browsed it

1. 类的继承扩展

33848-ma4nz6tsixf.png

  1. //类的继承/扩展
  2. class Demo1
  3. {
  4. private int $age = 10;
  5. //protected 受保护的,用在当前类或者他的子类中 外部不可见
  6. protected string $name;
  7. //魔术方法
  8. public function __construct($name)
  9. {
  10. $this->name = $name;
  11. }
  12. public function getUser(): string
  13. {
  14. return $this->name;
  15. }
  16. }
  17. //extends继承 将Demo1中的代码复制到Demo2中
  18. class Demo2 extends Demo1
  19. {
  20. //扩展父类中的属性
  21. public string $guoji;
  22. //扩展父类中的方法
  23. public function __construct($age,$name,$guoji)
  24. {
  25. //parent引用父类的方法
  26. parent::__construct($name);
  27. $this->age = $age;
  28. $this->guoji = $guoji;
  29. }
  30. //扩展父类的方法
  31. public function getUser():string
  32. {
  33. return '我是'. parent::getUser() . ',年龄是'.$this->age . ',国家是'.$this->guoji;
  34. }
  35. }
  36. $demo = new Demo2(20,'张三','中国');
  37. echo $demo->getUser();

2. 抽象类

抽象类声明关键字abstract 如果一个类中有抽象方法 那么这个类必须要被声明为抽象类

60204-lge72qsjyj.png

  1. //抽象类
  2. //抽象类声明关键字abstract
  3. //如果一个类中有抽象方法 那么这个类必须要被声明为抽象类
  4. abstract class Demo3
  5. {
  6. public static $name='admin';
  7. //将一个没有实现的方法声明为抽象方法
  8. abstract public function f($name);
  9. }
  10. //必须在子类中将抽象方法实现
  11. class Demo4 extends Demo3
  12. {
  13. public function f($name)
  14. {
  15. return '我是'.$name;
  16. }
  17. }
  18. $demo4 = new Demo4();
  19. echo $demo4->f('李四');

3.全局成员

  • 常量/类/接口/函数 全局成员
  • 全局成员不能重复声明全局有效
  • 用命名空间在一个文件内重复声明

23438-7lujlmhu0js.png

  1. // 2.全局成员有哪些
  2. /**
  3. * 函数/常量/类/接口
  4. * 全局成员不允许重复声明全局有效
  5. * 可以使用命名空间在一个文件内重复声明
  6. */
  7. namespace Demo1;
  8. const USER = '第一个';
  9. class User
  10. {
  11. }
  12. echo USER;
  13. echo '<hr>';
  14. namespace Demo2;
  15. const USER = '第二个';
  16. class User
  17. {
  18. }
  19. echo USER;

4.命名空间

63161-oe1o059oq0k.png

  1. //命名空间解决了全局成员的命名冲突
  2. //当存在命名空间时访问全局成员应该要用完整名称
  3. //可以通过\命名空间\的方式来访问其他空间的成员
  4. // namespace Demo;
  5. // echo \Demo2\User::$name;
  6. // 命名空间声明方式:
  7. // 1. 当前路径: 非限定名称, Index
  8. // 2. 相对路径: 限定名称, two\Index
  9. // 3. 绝对路径: 完全限定名称, \one\two\Index
  10. //如果命名空间太长比如是多级可以用关键字use进行修饰
  11. // use 默认使用完全限定名称的类名/绝对路径
  12. namespace Demo\Index;
  13. class User
  14. {
  15. public static $name = User::class.'\张三<hr>';
  16. }
  17. use \Demo\Index\User as UserIndex;
  18. echo UserIndex::$name;
  19. namespace Demo\Index1;
  20. use \Demo\Index1\User as UserIndex1;
  21. class User
  22. {
  23. public static $name = User::class.'\李四<hr>';
  24. }
  25. echo UserIndex1::$name;
Correcting teacher:PHPzPHPz

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
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!