Blogger Information
Blog 25
fans 0
comment 0
visits 29581
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP创建类、实例化、命名空间、类成员、类方法-大型CMS系统开发-第九期
宿州市筋斗云信息科技-Vip
Original
1235 people have browsed it

PHP创建类、实例化、命名空间、类成员、类方法

创建类和方法
  1. <?php
  2. // 类是命名空间
  3. namespace user;
  4. // 创建一个类
  5. class lists {
  6. //成员
  7. public $name = '李四';
  8. public $age = 36;
  9. //方法
  10. function getPeople() {
  11. $f = new self();
  12. return '姓名:'.$f->name .'<br>'.'年龄:'.$f->age;
  13. }
  14. }
  15. // 实例化类
  16. $people = new lists();
  17. // 输出值
  18. echo $people->getPeople();

类的构造方法
  1. // 创建一个类
  2. class lists {
  3. //成员
  4. // 属性
  5. public $name;
  6. public $age;
  7. // 构造方法
  8. public function __construct($name, $age){
  9. $this->name = $name;
  10. $this->age = $age;
  11. }
  12. //方法
  13. function getPeople() {
  14. return '姓名:'.$this->name .'<br>'.'年龄:'.$this->age;
  15. }
  16. }
  17. // 实例化类
  18. $people = new lists('李四',36);
  19. // 输出值
  20. echo $people->getPeople();

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