Blogger Information
Blog 43
fans 4
comment 0
visits 18988
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件包含/类与对象
汇享科技
Original
337 people have browsed it

文件包含

  • include require
  • 引入的文件和当前文件处在同一个作用域当中 如果出现同名变量会覆盖之前的变量
  • 区别:引入另一个文件如果有错误 include 会弹出警告错误 但是不会影响后续代码的执行require会抛出致命错误整个文件将无法运行

include引入方式

41791-p2nbl05sx8k.png

require引入方式

43443-afz1vt7xmeo.png

类与对象

  1. 声明类关键字class
  2. 类中的成员:私有成员private、公共成员public、静态成员static
  3. 实例化类跟js相同 new Demo() 访问类成员$demo->user使用->方式
  4. 魔术方法__get读取私有变量,__set修改变量__construct构造方法此方法会在实例化时自动调用

09308-frek7tcifo5.png

代码演示

  1. <?php
  2. namespace _0813;
  3. class User
  4. {
  5. //声明私有变量
  6. private $age = 18;
  7. private $gongzi = 2800;
  8. //声明公共变量
  9. public $username = '老王';
  10. //方法
  11. public function getAge(){
  12. return $this->age;
  13. }
  14. //魔术方法读
  15. public function __get($name){
  16. return $this->$name;
  17. }
  18. //魔术方法写
  19. public function __set($name,$v){
  20. $this->$name = $v;
  21. }
  22. }
  23. $user = new User();
  24. echo $user->username.'<br>';
  25. echo $user->getAge().'<br>';
  26. echo $user->gongzi.'<br>';
  27. $user->gongzi = 3800;
  28. echo $user->gongzi.'<hr>';
  29. //类中的构造方法
  30. class User1
  31. {
  32. private $chengji;
  33. private $age;
  34. public $username;
  35. //静态方法
  36. public static $guoji;
  37. //魔术方法实例化类时自动调用
  38. public function __construct($chengji,$age,$username,$guoji)
  39. {
  40. $this->chengji = $chengji;
  41. $this->age = $age;
  42. $this->username =$username;
  43. //使用关键字self初始化静态属性
  44. self::$guoji = $guoji;
  45. }
  46. public function __get($name){
  47. return $this->$name;
  48. }
  49. }
  50. $user1 = new User1('优秀',38,'小刘','中国');
  51. echo '我的名字:'.$user1->username.',我的年龄是:'.$user1->age.',我的祖国是:'.$user1::$guoji;
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!