Blogger Information
Blog 35
fans 3
comment 0
visits 25302
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php第九期 11月26日作业类的学习
随风
Original
667 people have browsed it

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

构造方法

`<?php
namespace admin;
class Demo {
public $product;
public $price;

  1. public function __construct($product,$price)
  2. {
  3. $this->product = $product;
  4. $this->price =$price;
  5. }
  6. // 对象方法
  7. public function getInfo(){
  8. return '品名: ' .$this->product .', 价格: ' . $this->price . '<br>';
  9. }
  10. // 析构方法: 在对象被删除/清理时自动调用
  11. public function __destruct()
  12. {
  13. echo '<h3 style="color:red">对象已被清理</h3>';
  14. }

}

$obj = new Demo(‘pc’,’2800’);
echo $obj-> getInfo();
// unset ($obj);

// $obj=null;
`

总结

学习了类的知识,对类有了一定的了解,希望能有案例进行深入了解一下。

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