Blogger Information
Blog 47
fans 1
comment 0
visits 40483
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件本质与作用域,实例演示关键字
新手1314
Original
617 people have browsed it

1.文件本质与作用域

1.include

  1. //文件本质:将目录文件复制到当前位置
  2. //在inc下的zuoye.php
  3. zuoye.php:
  4. <? php
  5. $name = '新手1314';
  6. $email = '新手1314@qq.com';
  7. return '新手@qq.com';
  8. ?>
  9. //在根目录下调用php
  10. <? php
  11. include __DIR__ . '/inc/zuoye.php';
  12. echo $name.'<br>';
  13. $myemail = include __DIR__ . '/inc/zuoye.php';
  14. echo $email;
  15. echo $myemail;
  16. ?>
  17. //输出结果分别为:新手1314 ,新手1314@qq.com , 新手@qq.com

2.require

  1. require __DIR__ .'/inc/zuoye.php';
  2. echo $name .'<br>';
  3. $myemail = require __DIR__ . '/inc/zuoye.php';
  4. echo $email;
  5. echo $myemail;
  6. //输出结果分别为:新手1314,新手1314@qq.com,新手@qq.com

3.inluede与require区别

  1. // include: 加载失败,继续执行不中断
  2. // requrie: 加载失败,中断退出
  3. @include __DIR__ . '/inc/xs1314.phh';
  4. echo 'include后面的代码'.'<br>';
  5. require __DIR__ .'/inc/xs1314.php';
  6. echo 'require后面的代码';
  7. //输出结果分别为:include后面的代码
  8. Warning: **************
  9. Fatal error:***********

2.关键字

  1. //1.class:声明类
  2. class zuoye2{}
  3. //2.new :类的实例化
  4. $god = new zuoye2;
  5. var_dump($god); //输出验证类
  6. //输出结果:object(zuoye2)#1 (0) { }
  7. //3.private:声明为私有, 实现封装
  8. //4.public:访问限制符(公共)
  9. class user{
  10. private $name;
  11. public function getUser{
  12. return $this ->name;
  13. }
  14. public function __construct(string $name){
  15. $this->name = $name;
  16. }
  17. }
  18. $user = new user('新手1314');
  19. echo $user->getUser();
  20. //输出结果为:新手1314
  21. //5.static:静态属性
  22. class user2{
  23. private static $name = '新手';
  24. public static function getUser(){
  25. return self::$name;
  26. }
  27. }
  28. echo user2::$getUser();
  29. //输出结果:新手
  30. //5.protected:继承, 可在本类或子类中使用, 不对外公开
  31. //6.extends:继承,可继承父类
  32. //7.parent:父类引用
  33. class person(){
  34. private $id = 1;
  35. protected $name;
  36. protected function getInfo(){
  37. return $this ->name;
  38. }
  39. public function __construct(){
  40. $this ->name=$name;
  41. }
  42. }
  43. class stu extends preson{
  44. private $lesson;
  45. private $score;
  46. public function __construct($name,$lesson,$score){
  47. parent::__construct($name);
  48. $this ->lesson = $lesson;
  49. this ->score =$score;
  50. }
  51. public function getInfo(){
  52. return parent::getInfo() ."同学,课程为$this->lesson的成绩为$this->score分";
  53. }
  54. }
  55. $stu = new stu('新手','php','60');
  56. echo $stu->getInfo();
  57. //输出结果为:新手同学,课程为php 的成绩为 60 分
  58. //8.abstract:抽象类,防止父类被直接调用,只能用继承
  59. abstract class demo {}
  60. class demo1 extends demo{}
  61. echo 'demo1的父类名为:'. get_parent_class(new demo1);
  62. //输出结果:demo1的父类是:demo
  63. //9.final:使用后不能被继承
  64. final class demo2{}
  65. class demo3 extends demo2{}
  66. //输出结果:Fatal error:*********
  67. //10.interface: 声明接口
  68. //11.implements: 实现接口(普通类必须将接口中的所有抽象方法全部实现,抽象类不需要所有类)
  69. interface jiekou{
  70. const NATION='CHINA';
  71. public function m1();
  72. public function m2();
  73. }
  74. //普通类
  75. class shixian implements jiekou{
  76. public function m1(){
  77. }
  78. public function m2(){
  79. }
  80. }
  81. //输出结果:
  82. //抽象类
  83. abstract class shixian2 implements jiekou{
  84. public function m1(){}
  85. }
  86. //输出结果:
  87. //使用接口实现多继承
  88. interface A{}
  89. interface B{}
  90. interface C{}
  91. class text implements A,B,C{}
  92. //? class_implements:返回指定的类实现的所有接口
  93. $arr = class_implements('text');
  94. printf('<pre>%s</pre>',print_r($arr,true));
  95. //输出结果:
  96. Array
  97. (
  98. [A] => A
  99. [B] => B
  100. [C] => C
  101. )
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