首页 >后端开发 >php教程 >PHP之构造析构

PHP之构造析构

WBOY
WBOY原创
2016-07-29 09:02:50866浏览

在实例化一个新对象时,构造方法和析构方法都会被自动调用,若有继承则会使用父类的对应方法。

  • 析构方法在三种情况下会被调用:

    • 使用unset()销毁一个对象,若存在对象传值则不会被调用;
    • 改变变量指向对象的值;
    • php程序代码运行结束后。
<code><span><span><?php </span><span><span>class</span><span>base</span>{</span><span>public</span><span>$name</span>;

    <span><span>function</span><span>__construct</span><span>(<span>$name</span>)</span>{</span><span>$this</span>->name = <span>$name</span>;
        <span>echo</span><span>'obj '</span>.<span>$this</span>->name.<span>' have built'</span>.<span>''</span>.<span>''</span>;
    }
    <span><span>function</span><span>__destruct</span><span>()</span>{</span><span>echo</span><span>'obj '</span>.<span>$this</span>->name.<span>' have destroyed'</span>.<span>''</span>.<span>''</span>;
    }
}

<span>$a</span> = <span>new</span> base(<span>'a'</span>);
<span>$b</span> = <span>new</span> base(<span>'b'</span>);
<span>$c</span> = <span>new</span> base(<span>'c'</span>);

<span>unset</span>(<span>$b</span>);
<span>$c</span> = <span>'d'</span>;</span></span></code>

PHP之构造析构

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了PHP之构造析构,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn