Blogger Information
Blog 36
fans 1
comment 0
visits 28844
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月26日类与对象及构造方法-九期线上班
WJF
Original
551 people have browsed it

创建类、实例化、命名空间、类成员、类方法(手写)


  1. <?php
  2. //创建类
  3. class demo{
  4. }
  5. //调用类/实例化
  6. $d1 = new demo();
  7. $d2 = new demo();
  8. var_dump($d1);
  9. var_dump($d2);
  10. var_dump($d1 instanceof demo);
  11. //类属性
  12. class demo1{
  13. public $a ='aa';
  14. public $s = 111;
  15. }
  16. $aa = new demo1;
  17. echo $aa->a . $aa->s;
  18. echo '<hr>属性重新赋值';
  19. $aa->s = 222;
  20. echo $aa->a . $aa->s;
  21. //行为(方法)
  22. class demo2 {
  23. // //属性
  24. public $name = 'WJF';
  25. public $mail= '33703259@qq.com';
  26. // //方法
  27. function test1 (){
  28. //self:当前类
  29. $dangqian = new self();
  30. //$this 伪变量 引用当前类中的实例
  31. return '姓名:' . $dangqian->name . '邮箱:' . $this->mail;
  32. }
  33. }
  34. $d2 = new demo2();
  35. echo $d2->test1();



构造方法


  1. <?php
  2. //创建类
  3. class Demo{
  4. //属性
  5. public $a;
  6. public $s;
  7. //构造方法
  8. function __construct($a,$s)
  9. {
  10. $this->a = $a;
  11. $this->s = $s;
  12. }
  13. //方法
  14. function tset(){
  15. echo $this->a;
  16. }
  17. }
  18. $obj = new Demo('啊啊啊',333);
  19. echo $obj->tset();

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