Blogger Information
Blog 34
fans 0
comment 0
visits 24341
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP基础 -(五)include,require,类,访问限制符
CY明月归
Original
917 people have browsed it
作业内容:
1. 文件包括的本质与作用域,实例演示;

  1. <?php
  2. //使用require加载通用部分
  3. //require 和 include 几乎完全一样,除了处理失败的方式不同之外。
  4. //require 在出错时产生 E_COMPILE_ERROR 级别的错误。
  5. //换句话说将导致脚本中止而 include 只产生警告(E_WARNING),脚本会继续运行。
  6. require __DIR__ . '/mysite/header.php';
  7. $content = <<<body
  8. <h1 style ="background:gray;color:white;height:300px;text-align:center">this is a content</h1>
  9. body;
  10. echo $content;
  11. require __DIR__ . '/mysite/footer.php';
2. 将课堂上讲到的所有关键字,全部实例演示一遍class/new/extends/public/private/protected/interface/abstruct…

  1. class User{
  2. //访问限制符:
  3. //1. private: 私有,仅限在当前类中使用
  4. private $myname = 'zolo';
  5. public function sayHello(){
  6. return $this->myname;
  7. }
  8. }
  9. //如果一个类没有构造函数,以及构造函数的参数不是必填项时,括号就可以省略
  10. $u = new User;
  11. //打印zolo
  12. echo $u->sayHello();
  13. class User{
  14. private $myname = 'zolo';
  15. private $a;
  16. public function sayHello(){
  17. return $this->myname;
  18. }
  19. public function __construct($args){
  20. $this->myname = $args;
  21. }
  22. public function printa(){
  23. //在方法内赋值
  24. return $this->a = 'zolo2';
  25. }
  26. }
  27. //重新给myname赋值
  28. $u = new User('zolo1');
  29. //打印zolo1
  30. echo $u->sayHello();
  31. //打印zolo2
  32. echo $u->printa();
  33. /**
  34. * 类的扩展/抽象/最终
  35. * 1. protected: 受保护/可继承
  36. * 2. extends: 扩展/继承
  37. * 3. parent: 父类引用
  38. * 4. abstract: 抽象
  39. * 5. final: 最终类
  40. */
  41. class Person{
  42. // public: 类中,子类, 类外都可见
  43. public $str = 'yes ! I am public';
  44. // private: 私有, 仅限当前类, 子类,外部都不可见
  45. private $num = 9999;
  46. // protected: 成员可继承,可以在子类中使用
  47. protected $canwalk = 'yes ! preson can walk';
  48. protected $name;
  49. public function __construct($arg)
  50. {
  51. //给protected $name赋值
  52. $this->name = $arg;
  53. }
  54. protected function getInfo(){
  55. return $this->name;
  56. }
  57. }
  58. class Student extends Person{
  59. private $score;
  60. public function __construct($name,$score)
  61. {
  62. // 引用了父类的构造方法
  63. // parent: 父类引用 Person
  64. parent::__construct($name);
  65. $this->score = $score;
  66. }
  67. public function getStuInfo(){
  68. return '调用父类方法:'.parent::getInfo().'<br>';
  69. }
  70. public function getScore(){
  71. return $this->name.'同学的成绩是'.$this->score.'<br>';
  72. }
  73. public function getFatherStr(){
  74. return '调用父类属性'.$this->canwalk.'<br>';
  75. }
  76. }
  77. // $a = new Stu('NAMEA');
  78. // echo $a->getInfo(); potected改成public就能在类外访问
  79. $zolo = new Student('zolo',100);
  80. echo $zolo->getStuInfo();
  81. echo $zolo->getScore();
  82. echo $zolo->getFatherStr();
  83. // var_dump打印属性
  84. //var_dump($zolo);
  85. //美化打印
  86. printf('<pre>%s</pre>',print_r($zolo,true));
  87. // 抽象类必须通过继承/扩展的子类来间接实例化,不能直接使用
  88. abstract class Demo{
  89. }
  90. class D extends Demo{
  91. }
  92. $d = new D;
  93. //echo get_parent_class($d);//打印Demo
  94. //抽象方法: 只有方法名,参数列表,没有具体实现(大括号);
  95. //任何一个类,如果它里面至少有一个方法是被声明为抽象的,那么这个类就必须被声明为抽象的
  96. abstract class E{
  97. abstract protected function hello($arg);
  98. }
  99. class F extends E{
  100. public function hello($arg){
  101. return 'Hello,abstract function implement:'.$arg.'<br>';
  102. }
  103. }
  104. //call_user_func( callable $callback [, mixed $... ]): mixed
  105. echo call_user_func([new F,'hello'],'zolo');
  106. //final 关键字通过在定义方法和常量之前加上 final 来防止被子类覆盖
  107. //如果一个类被声明为 final,则不能被继承。
  108. //为了防止被继承, 可声明为最终类
  109. final class G{
  110. }
  111. // 声明会报错
  112. // class H extends G{
  113. // }
  114. interface userPay{
  115. // 1. 类常量
  116. const PAYKEY = 'zolo';
  117. // 2. 必须是抽象,必须是public
  118. public function weChat();
  119. public function aliPay();
  120. }
  121. interface userBank{
  122. public function zsBank();
  123. }
  124. class testPay implements userPay,userBank{
  125. public function weChat(){}
  126. public function aliPay(){}
  127. public function zsBank(){}
  128. }
  129. printf('<pre>%s</pre>',print_r(class_implements('testPay'),true));
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!