时间:2021-07-01 10:21:17 帮助过:3人阅读
<?php//构造方法和析构方法
class guests{
private $name;
private $gender;
function __construct($name,$gender){ //构造方法__construct 对属性进行初始化,构造方法不能返回return值
$this->name=$name; //访问类中成员的属性 $变量名->成员属性=值
$this->gender=$gender;
}
function get_name(){
return $this->name;
}
function get_gender(){
return $this->gender;
}
}
$people1 = new guests("刘备","男"); //在实例化的同时对此实例(对象)赋值
$people2 = new guests("关羽","女");
echo $people1->get_name()."\t".$people1->get_gender()."<br/>";
echo $people2->get_name()."\t".$people2->get_gender();
function __destruct(){ //析构方法__destruct 在对象销毁的时候调用执行
//方法的内容,通常完成一些在对象销毁前的清理任务
}相关文章推荐:
PHP中常用的函数以及全局变量的总结(推荐)
PHP如何实现计数排序的代码
以上就是php中构造方法和析构方法的代码实现的详细内容,更多请关注Gxl网其它相关文章!