Blogger Information
Blog 43
fans 0
comment 3
visits 27038
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP:1. 文件包含的本质与作用域,实例演示; 2. 类相关实例演示
Time
Original
540 people have browsed it

1. 文件包含的本质与作用域,实例演示

概括:

  1. //1.文件包含的本质:将目录文件复制到当前位置
  2. //2.被包含文件共享作用域
  3. //用include和require引入文件
  4. //可以在当前文件调用引入文件的内容
  5. include(想要引入的文件绝对路径);
  6. require(想要引入的文件绝对路径);

演示:

include

2. 类相关实例演示

概括:

  1. 一、常规成员
  2. 1.属性:可被其他方法共享
  3. 2.方法
  4. 方法分为:自定义方法和魔术方法(由系统调用,用户不能直接调用;
  5. 访问限制符
  6. 1.private:私有的,仅在当前类使用
  7. 2.protected:继承,可在本类或者子类使用,不对外公开
  8. 3.public:公开的,都可以访问(默认值)
  9. 二、静态成员
  10. 1.static:静态属性、静态方法
  11. 2.与类实例无关,最好不用$this调用
  12. 类的扩展:抽象类、最终类
  13. 1.protected:受保护的/可继承
  14. 2.extends:扩展/继承
  15. 3.parent:父类引出
  16. 4.abstract:抽象
  17. 5.final:最终
  18. 接口:大号抽象类,使用关键字interface定义,
  19. 使用关键字implements继承。

代码实例:

  1. <?php
  2. //常规
  3. class User
  4. {
  5. private $name;
  6. protected $age;
  7. public $email;
  8. public function getName(){
  9. return $this->name;
  10. }
  11. public function __construct(string $name){
  12. $this->name = $name;
  13. }
  14. }
  15. // $user = new User('zhu');
  16. // echo $user->getName();
  17. //静态
  18. class User1{
  19. private static $name = '静态';
  20. public static function getName(){
  21. return self::$name;
  22. }
  23. }
  24. // echo User1::getName();
  25. //类的扩展:抽象类/最终类
  26. class Preson{
  27. protected $name;
  28. private $id;
  29. public function __construct(string $name){
  30. $this->name = $name;
  31. }
  32. protected function getInfo(){
  33. return $this->name;
  34. }
  35. }
  36. //Stu类 继承Preson类:关键字extends
  37. class Stu extends Preson{
  38. // 1.属性扩展
  39. private $lesson;
  40. private $score;
  41. // 2.方法扩展和重写
  42. public function __construct($name,$lesson,$score){
  43. parent::__construct($name);
  44. $this->lesson =$lesson;
  45. $this->score =$score;
  46. }
  47. function getInfo(){
  48. return parent::getInfo() . $this->lesson . $this->score;
  49. }
  50. }
  51. // $stu = new Stu('name','课程哦','88分');
  52. // echo $stu->getInfo();
  53. //abstract:抽象类只能继承
  54. abstract class Demo{
  55. }
  56. class Demo1 extends Demo{
  57. }
  58. // echo 'Demo1的父类是' . get_parent_class(new Demo1);
  59. abstract class Demo2{
  60. //抽象方法:只有方法名,没有具体实现
  61. abstract protected function hello($name);
  62. }
  63. class Demo3 extends Demo2{
  64. //必须实现父类中的抽象成员
  65. public function hello($name){
  66. return 'Hello' . $name;
  67. }
  68. }
  69. // echo call_user_func([new Demo3,'hello'],'猪');
  70. //最终类final: 不能被继承,只能实例使用
  71. final class Demo5{
  72. }
  73. //接口:大号抽象类
  74. interface iUser{
  75. //1.类常量
  76. const NATION = 'CHIAN';
  77. // 2.必须是抽象,必须是public
  78. public function m1();
  79. public function m2();
  80. }
  81. //接口不能直接用,要用一个类实现
  82. // 1.用普通类实现,必须将接口所有抽象方法实现
  83. class Test implements iUser{
  84. public function m1(){
  85. return self::NATION;
  86. }
  87. public function m2(){}
  88. }
  89. echo call_user_func([new Test,'m1']);
  90. // 2.用抽象类实现,允许部分实现
  91. abstract class Test1 implements iUser{
  92. public function m1(){
  93. }
  94. }
  95. //可通过接口实现多继承
  96. interface A{}
  97. interface B{}
  98. interface C{}
  99. class Test3 implements A,B,C{}
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