在實例化一個新物件時,建構方法和析構方法都會自動調用,若有繼承則會使用父類別的對應方法。
析構方法在三種情況下會被呼叫:
<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教程有興趣的朋友有幫助。 🎜 🎜 🎜