构造方法:
class text { private $name; private $gender; public function __construct($a="小明",$b="小红") //一个类只能有一个构造方法 { echo $this->name = $a; //构造方法不能返回(return)值 echo $this->gender = $b; } } //在实例化类的同时自动调用构造方法 常用于属性的初始化 $ming = new text(); $hong = new text("小李","大黄"); //构造方法 实例化时赋值 /* 运行结果: 小明小红小李大黄 */
class text { private $name; private $gender; public function __construct($a="小明",$b="小红") { $this->name = $a; $this->gender = $b; } public function get_name(){ return $this->name; } public function get_gender(){ echo $this->gender; } } $text = new text(); echo $text->get_name(); $text->get_gender(); $text = new text("小李","小齐"); echo $text->get_name(); $text->get_gender();
析构方法:
function __destruct(){ //方法内容,通常用于完成一些对象销毁前的清理任务 }