Blogger Information
Blog 33
fans 0
comment 0
visits 19766
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月26日作业构造方法--PHP培训九期线上班
取个名字真难
Original
545 people have browsed it

构造方法

  1. <?php
  2. class person
  3. {
  4. public $name;
  5. public $age;
  6. public $sex;
  7. //构造方法__construct 用于初始化程序
  8. public function __construct($name, $age, $sex)
  9. {
  10. $this->name = $name;
  11. $this->age = $age;
  12. $this->sex = $sex;
  13. $this->say();
  14. }
  15. function say()
  16. {
  17. echo '我的名字是:' . $this->name . '<br>';
  18. echo '我的年纪是:' . $this->age . '<br>';
  19. echo '我的性别是:' . $this->sex . '<hr>';
  20. }
  21. //析构方法 __destruct() 在对象被销毁时,被自动调用的一个方法
  22. public function __destruct()
  23. {
  24. echo $this->name.'说完了哦<br>';
  25. }
  26. }
  27. //实例化对象
  28. $p1=new person('张三',20,'男');
  29. $p2=new person('李四',18,'女');
  30. echo $p2->name;
  31. echo '<br>';
  32. echo $p1->age;
  33. echo '<br>';

显示效果

Correcting teacher:查无此人查无此人

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