Rumah > Artikel > pembangunan bahagian belakang > Pembina dalam PHP
Editor PHP Xiaoxin akan memberi anda analisis terperinci tentang pembina dalam PHP. Pembina ialah kaedah yang dipanggil secara automatik apabila objek dimulakan untuk memulakan sifat objek. Melalui pembina, anda boleh lulus parameter dan menetapkan nilai kepada sifat apabila mencipta objek, meningkatkan kebolehbacaan dan kebolehgunaan semula kod. Nama pembina adalah sama dengan nama kelas, dan tidak perlu memanggilnya secara manual, yang memudahkan pengurusan bersatu dan penyelenggaraan kod. Dalam PHP, konstruktor adalah bahagian penting dalam pengaturcaraan berorientasikan objek Pemahaman mendalam dan aplikasi rasional pembina boleh meningkatkan kualiti dan kecekapan kod.
Kami juga akan menggunakan fungsi ini untuk memulakan sifat objek dalam kelas dengan parameter yang diberikan.
Akhir sekali, kita akan melihat cara membina fungsi dalam 子类
中启动对象并在两个类都有单独的构造函数时调用父类
.
Dalam contoh berikut, kami akan mencipta kelas Student
并使用 __construct
函数为 new Student
dan menetapkan sifatnya.
__construct
函数减少了与使用函数 set_name()
Bilangan kod yang berkaitan.
<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>
Keluaran:
<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
Dalam contoh kod di bawah, kami mencipta fungsi kelas Military
并使用 __construct
untuk menyediakan sifat dan parameter objek yang kami cipta.
<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>
Keluaran:
<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
apabila kedua-dua kelas mempunyai <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>
Keluaran:
<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
Pembina kelas.
Atas ialah kandungan terperinci Pembina dalam PHP. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!