【PHP构造方法和析构方法的使用】

时间:2022-09-27 17:22:41

构造方法:__construct,析构方法:__destruct

代码示例:

 <?php
class Person
{
public $name;
public $age;
public function __construct($name,$age)
{
$this->name=$name;
$this->age=$age;
}
public function showInformation()
{
echo "<br/>姓名:".$this->name;
echo "<br/>年龄:".$this->age;
}
public function __destruct()
{
echo "<br/>释放资源".$this->name;
}
}
$zhangsan=new Person("张三",12);
//$temp=$zhangsan;
$zhangsan=null;
$lisi=new Person("李四",13);
$wangwu=new Person("王五",14);
?>