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