Blogger Information
Blog 33
fans 0
comment 0
visits 17092
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件包含的本质与作用域,类的声明/扩展/抽象/最终/接口
lucaslwk
Original
437 people have browsed it

文件包含的本质与作用域,类的声明/扩展/抽象/最终/接口

文件包含

  1. <?php
  2. //文件包含
  3. //? 文件包含的本质:将外部文件内容复制到当前文档中
  4. //? 文件包含中的变量共享当前作用域
  5. //include 动态,用到时再加载
  6. //require 静态,应写到顶部;加载失败,中断退出
  7. include __DIR__ . '/out/demo1.php';
  8. echo $email . '<br>';
  9. require __DIR__ . '/out/demo2.php';
  10. echo $name . '<br>';
  11. //include 加载失败,继续执行不中断
  12. include __DIR__ . '/out/demo3.php';
  13. echo $name . '<br>';
  14. //require 加载失败,中断退出
  15. require __DIR__ . '/out/demo3.php';
  16. echo $name . '<br>';

类的声明,类成员访问限制符

class,new,private,protected,public,static,$this,self

  1. <?php
  2. //常规成员
  3. class Person
  4. {
  5. //私有变量,仅限在当前类中使用
  6. private $name;
  7. //可继承, 可在本类或子类中使用, 不对外公开
  8. protected $province;
  9. //公共
  10. public $country = 'China';
  11. public function getName()
  12. {
  13. //$this引用当前类
  14. return $this->name;
  15. }
  16. //魔术方法,系统调用,实例化自动调用
  17. public function __construct($name)
  18. {
  19. $this->name = $name;
  20. }
  21. }
  22. $user1 = new Person('张三');
  23. //echo $user1->name.'<br>';
  24. //echo $user1->province.'<br>';
  25. echo $user1->country . '<br>';
  26. echo $user1->getName() . '<br>';
  27. //静态成员
  28. class Person2
  29. {
  30. //静态属性
  31. private static $name = 'Lily';
  32. //类常量: 与实例无关,用类访问
  33. const COUNTRY = 'USA';
  34. //静态方法
  35. public static function getName()
  36. {
  37. //引用当前类
  38. return self::$name;
  39. }
  40. }
  41. echo Person2::COUNTRY . '<br>';
  42. echo Person2::getName() . '<br>';

protected,extends,abstract,final,parent

  1. <?php
  2. //类的扩展/抽象/最终
  3. //protected,extends,abstract,final,parent
  4. class Person
  5. {
  6. private $name;
  7. protected $province;
  8. protected function getName()
  9. {
  10. return $this->name;
  11. }
  12. protected function __construct($name)
  13. {
  14. $this->name = $name;
  15. }
  16. }
  17. class Student extends Person
  18. {
  19. //属性的扩展
  20. private $lesson;
  21. private $score;
  22. //重写方法
  23. public function __construct($name, $lesson, $score)
  24. {
  25. //parent引用父类
  26. parent::__construct($name);
  27. $this->lesson = $lesson;
  28. $this->score = $score;
  29. }
  30. //扩展方法
  31. public function getInfo()
  32. {
  33. return parent::getName() . " $this->lesson 课程为 $this->score 分";
  34. }
  35. }
  36. $student1 = new Student('张三', 'php', '99');
  37. echo $student1->getInfo() . '<br>';
  38. //抽象类,不想让用户直接使用父类
  39. abstract class Demo1
  40. {
  41. //抽象方法:只有方法名,参数列表,没有具体实现{}
  42. abstract protected function hello($name);
  43. }
  44. // new Demo1;
  45. //最终类,无法被继承/扩展
  46. // final class Demo1
  47. // {
  48. // }
  49. class Demo2 extends Demo1
  50. {
  51. //工作类中必须实现父类中的抽象成员
  52. public function hello($name): string
  53. {
  54. return 'Hello , ' . $name;
  55. }
  56. }
  57. echo get_parent_class(new Demo2) . '<br>';
  58. echo call_user_func([new Demo2, 'hello'], '牛老师');

interface,implements

  1. <?php
  2. //interface,implements
  3. interface A
  4. {
  5. //类常量
  6. const SOURCE = 'PHP';
  7. //必须抽象,必须public
  8. //!不建议声明抽象构造方法
  9. public function m1();
  10. public function m2();
  11. }
  12. interface B
  13. {
  14. }
  15. interface C
  16. {
  17. }
  18. class User implements A, B, C
  19. {
  20. //普通类实现,须实现所有抽象成员
  21. public function m1()
  22. {
  23. }
  24. public function m2()
  25. {
  26. }
  27. }
  28. abstract class User2 implements A, B, C
  29. {
  30. //抽象类实现,允许有不实现的抽象成员
  31. public function m1()
  32. {
  33. }
  34. }
  35. //查看当前类实现的所有接口
  36. print_r(class_implements('User'));
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