构造函数:constructor
析构函数:destructor
在php5.1中,构造函数统一命名为:
function __construct(){
#函数体
}
析构函数统一命名为:
function __destruct(){
#函数体
}
注意,construct和destruct之前有两个下划线,不是一个
下面给出一个构造函数的示例:
<?php
class Car{
private $color;
private $size;
private $price;
function __construct(){
$this->color="red";
$this->size=4;
$this->price=30000;
}
public function display(){
echo $this->color."<br>";
echo $this->size."<br>";
echo $this->price."<br>";
}
}
$car=new Car();
$car->display();
?>
析构函数:destructor
在php5.1中,构造函数统一命名为:
function __construct(){
#函数体
}
析构函数统一命名为:
function __destruct(){
#函数体
}
注意,construct和destruct之前有两个下划线,不是一个
下面给出一个构造函数的示例:
<?php
class Car{
private $color;
private $size;
private $price;
function __construct(){
$this->color="red";
$this->size=4;
$this->price=30000;
}
public function display(){
echo $this->color."<br>";
echo $this->size."<br>";
echo $this->price."<br>";
}
}
$car=new Car();
$car->display();
?>
本文介绍了PHP中构造函数和析构函数的概念及其使用方法。构造函数__construct用于初始化类的实例,而析构函数__destruct则在对象销毁时执行。通过示例展示了如何定义这些函数并在类中应用。
619

被折叠的 条评论
为什么被折叠?



