在实例化一个新对象时,构造方法和析构方法都会被自动调用,若有继承则会使用父类的对应方法。
析构方法在三种情况下会被调用:
<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>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介绍了PHP之构造析构,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。