在 PHP4 中,当函数与对象同名时,这个函数将成为该对象的构造函数,并且在 PHP4 中没有析构函数的概念.
在 PHP5 中,构造函数被统一命名为 __construct,并且引入了析构函数的概念,被统一命名为 __destruct.
例一:构造函数和析构函数 www.444p.com
PHP代码
- <?
- class foo {
- var $x;
- function __construct($x) {
- $this->x = $x;
- }
- function display() {
- print($this->x);
- }
- function __destruct() {
- print("bye bye");
- }
- }
- $o1 = new foo(4);
- $o1->display();
- ?>
www.444p.com php学习之家
在上面的例子中,当你终止调用 foo 类的时候,其析构函数将会被调用,上例中会输出 “bye bye”. 本文来自 www.444p.com