ASCII码 ASCII码

文件包括的本质与作用域,实例演示&演示关键字

发布于:2022-04-26 17:09:20  栏目:技术文档

1

  1. <?php
  2. // 1. 文件包括的本质与作用域,实例演示;
  3. include __DIR__ . "/static/p1.php";
  4. echo $username . "<hr>";
  5. require __DIR__ . "/static/p1.php";
  6. echo $username . "<hr>";
  7. echo "Hello " . include __DIR__ . "/static/p1.php";
  8. echo "<br/>";

p1

  1. <?php
  2. $username = "Dave";
  3. return "php.cn";

2

  1. <?php
  2. // 2. 将课堂上讲到的所有关键字,全部实例演示一遍
  3. // class/new/extends/public/private/protected/interface/abstruct...
  4. //final
  5. final class numberPhp{
  6. private $Pi = 3.1415926;
  7. public function getPi(){
  8. return $this->Pi;
  9. }
  10. }
  11. $np = new numberPhp;
  12. echo $np->getPi();
  13. echo "<hr>";
  14. //class / new
  15. //public,private,protected
  16. class phpcn{
  17. const ClassVersion = "19";
  18. private $course;
  19. protected $student;
  20. public $coursePlan;
  21. public static $teacher="猪老师";
  22. public function __construct(string $course, string $student,string $coursePlan){
  23. $this->course = $course;
  24. $this->student = $student;
  25. $this->coursePlan = $coursePlan;
  26. }
  27. public function getInfo(){
  28. return $this->course . "include " . $this->student . " .And crouse plan " . $this->coursePlan;
  29. }
  30. public static function getTeacher(){
  31. return self::$teacher;
  32. }
  33. }
  34. $CClass = new phpcn("php ","Dave, John", "js,php.");
  35. echo $CClass->getInfo();
  36. echo "<hr>";
  37. echo phpcn::getTeacher();
  38. echo "<hr>";
  39. // extends
  40. class Dave extends phpcn{
  41. private $age = 20;
  42. public function getInfo(){
  43. return parent::getInfo()." .Also, Dave age is " . $this->age;
  44. }
  45. }
  46. $dave1 = new Dave("php ","Dave, John", "js,php.");
  47. echo $dave1->getInfo();
  48. echo "<hr>";
  49. // abstract
  50. abstract class Person{
  51. abstract public function age();
  52. }
  53. class mingming extends Person{
  54. private $age = 10;
  55. public function age(){
  56. return $this->age;
  57. }
  58. }
  59. $mm = new mingming;
  60. echo $mm->age();
  61. echo "<hr>";
  62. // interface,implements
  63. interface human{
  64. public function eat();
  65. }
  66. class xiaowang implements human{
  67. private $obj = "汉堡";
  68. public function eat(){
  69. return "喜欢吃" . $this->obj;
  70. }
  71. }
  72. $xw = new xiaowang;
  73. echo $xw->eat();
  74. echo "<hr>";
相关推荐
阅读 +