php小編小新為您詳細解析PHP中的建構子。建構子是在實例化物件時自動呼叫的方法,用來初始化物件的屬性。透過建構函數,可以在建立物件時傳遞參數並對屬性進行賦值,提高程式碼的可讀性和重用性。建構函數的命名與類別名稱相同,且不需要手動調用,以便統一管理和維護程式碼。在PHP中,建構函數是物件導向程式設計的重要組成部分,深入了解並合理地應用建構函數,能夠提升程式碼的品質與效率。
我們還將使用該函數來初始化類別中具有給定參數的物件的屬性。
最後,我們將看到如何在子類別
中啟動物件並在兩個類別都有單獨的建構函式時呼叫父類別
建構子。
在下面的範例中,我們將建立一個類別 Student
並使用 __construct
函數為 new Student
指派其屬性。
__construct
函數減少了與使用函數 set_name()
相關的程式碼數。
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Student</span> { </span></span><span style="display:flex;"><span> <span style="color:#408080;font-style:italic">// Define the attributes of your class </span></span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$name</span>; </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$em<strong class="keylink">ai</strong>l</span>; </span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">// Initialize the properties of the object you want to create in this class </span></span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">funct<strong class="keylink">io</strong>n</span> __construct(<span style="color:#19177c">$name</span>, <span style="color:#19177c">$email</span>) { </span></span><span style="display:flex;"><span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span> <span style="color:#666">=</span> <span style="color:#19177c">$name</span>; </span></span><span style="display:flex;"><span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">email</span> <span style="color:#666">=</span> <span style="color:#19177c">$email</span>; </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">get_name</span>() { </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">return</span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span>; </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">get_email</span>() { </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">return</span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">email</span>; </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span><span style="color:#19177c">$obj</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Student(<span style="color:#ba2121">"John"</span>, <span style="color:#ba2121">"john567@gmail.com"</span>); </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$obj</span><span style="color:#666">-></span><span style="color:#7d9029">get_name</span>(); </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"<br>"</span>; </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$obj</span><span style="color:#666">-></span><span style="color:#7d9029">get_email</span>(); </span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span > </span></span></span></code></code>
輸出:
<code> <code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>John </span></span><span style="display:flex;"><span>john567@gmail.com </span></span></code></code>
Object with Parameters
的屬性
在下面的範例程式碼中,我們建立類別 Military
並使用 __construct
函數來提供我們建立的物件的屬性和參數。
<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Military</span> { </span></span><span style="display:flex;"><span> <span style="color:#408080;font-style:italic">// Define the attributes of the class 'Military' </span></span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$name</span>; </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$rank</span>; </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">function</span> __construct(<span style="color:#19177c">$name</span>, <span style="color:#19177c">$rank</span>){ </span></span><span style="display:flex;"><span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span> <span style="color:#666">=</span> <span style="color:#19177c">$name</span>; </span></span><span style="display:flex;"><span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">rank</span> <span style="color:#666">=</span> <span style="color:#19177c">$rank</span>; </span></span><span style="display:flex;"><span> } </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">show_detail</span>() { </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span><span style="color:#666">.</span><span style="color:#ba2121">" : "</span>; </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Your Rank is "</span><span style="color:#666">.</span><span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">rank</span><span style="color:#666">.</span><span style="color:#ba2121">"</span><span style="color:#b62;font-weight:bold">\n</span><span style="color:#ba2121">"</span>; </span></span><span style="display:flex;"><span> } </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span><span style="color:#19177c">$person_obj</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Military(<span style="color:#ba2121">"Michael"</span>, <span style="color:#ba2121">"General"</span>); </span></span><span style="display:flex;"><span><span style="color:#19177c">$person_obj</span><span style="color:#666">-></span><span style="color:#7d9029">show_detail</span>(); </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"<br>"</span>; </span></span><span style="display:flex;"><span><span style="color:#19177c">$person2</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Military(<span style="color:#ba2121">"Fred"</span>, <span style="color:#ba2121">"Commander"</span>); </span></span><span style="display:flex;"><span><span style="color:#19177c">$person2</span><span style="color:#666">-></span><span style="color:#7d9029">show_detail</span>(); </span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span > </span></span></span></code></code>
輸出:
<code> <code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Michael : Your Rank is General </span></span><span style="display:flex;"><span>Fred : Your Rank is Commander </span></span></code></code>
Individual
建構子時呼叫父類別建構子<code> <code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Student</span> </span></span><span style="display:flex;"><span>{ </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$name</span>; </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">public</span> <span style="color:#008000;font-weight:bold">function</span> __construct(<span style="color:#19177c">$name</span>) </span></span><span style="display:flex;"><span> { </span></span><span style="display:flex;"><span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span> <span style="color:#666">=</span> <span style="color:#19177c">$name</span>; </span></span><span style="display:flex;"><span> } </span></span><span style="display:flex;"><span>}<span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Identity</span> <span style="color:#008000;font-weight:bold">extends</span> Student </span></span><span style="display:flex;"><span>{ </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$identity_id</span>; </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">public</span> <span style="color:#008000;font-weight:bold">function</span> __construct(<span style="color:#19177c">$name</span>, <span style="color:#19177c">$identity_id</span>) </span></span><span style="display:flex;"><span> { </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">parent</span><span style="color:#666">::</span><span style="color:#7d9029">__construct</span>(<span style="color:#19177c">$name</span>); </span></span><span style="display:flex;"><span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">identity_id</span> <span style="color:#666">=</span> <span style="color:#19177c">$identity_id</span>; </span></span><span style="display:flex;"><span> } </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">show_detail</span>() { </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span><span style="color:#666">.</span><span style="color:#ba2121">" : "</span>; </span></span><span style="display:flex;"><span> <span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Your Id Number is "</span><span style="color:#666">.</span><span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">identity_id</span><span style="color:#666">.</span><span style="color:#ba2121">"</span><span style="color:#b62;font-weight:bold">\n</span><span style="color:#ba2121">"</span>; </span></span><span style="display:flex;"><span> } </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span><span style="color:#19177c">$obj</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Identity(<span style="color:#ba2121">'Alice'</span>, <span style="color:#ba2121">'1036398'</span>); </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$obj</span><span style="color:#666">-></span><span style="color:#7d9029">show_detail</span>(); </span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span > </span></span></span></code></code>
輸出:
<code> <code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Alice : Your Id Number is 1036398 </span></span></code></code>
Identity
類別擴展了上述程式碼中的 Student
類別。我們使用關鍵字 parent:
來呼叫 Student
類別的建構子。
以上是PHP 中的建構函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!