PHP5中引入了新的统一的构造函数和析构函数名字:
<?php
class BaseClass {
function __construct() {
print "基类构造函数\n";
}
}
class SubClass extends BaseClass {
function __construct() {
parent::__construct();//需要明确调用,默认不会调用的
print "子类构造函数\n";
}
}
$obj = new BaseClass();echo "<br/>";
$obj = new SubClass();
?>
class BaseClass {
function __construct() {
print "基类构造函数\n";
}
}
class SubClass extends BaseClass {
function __construct() {
parent::__construct();//需要明确调用,默认不会调用的
print "子类构造函数\n";
}
}
$obj = new BaseClass();echo "<br/>";
$obj = new SubClass();
?>
析构函数__destruct():
<?php
class MyDestructableClass {
function __construct() {
print "构造函数\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "销毁 " . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
?>
class MyDestructableClass {
function __construct() {
print "构造函数\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "销毁 " . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
?>
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/106124,如需转载请自行联系原作者