Heim > Artikel > Backend-Entwicklung > Konstruktor in PHP
Der PHP-Editor Xiaoxin gibt Ihnen eine detaillierte Analyse des Konstruktors in PHP. Ein Konstruktor ist eine Methode, die automatisch aufgerufen wird, wenn ein Objekt instanziiert wird, um die Eigenschaften des Objekts zu initialisieren. Über den Konstruktor können Sie beim Erstellen eines Objekts Parameter übergeben und Eigenschaften Werte zuweisen, wodurch die Lesbarkeit und Wiederverwendbarkeit des Codes verbessert wird. Der Name des Konstruktors ist derselbe wie der Klassenname und es ist nicht erforderlich, ihn manuell aufzurufen, was eine einheitliche Verwaltung und Wartung des Codes erleichtert. In PHP sind Konstruktoren ein wichtiger Bestandteil der objektorientierten Programmierung. Ein tiefes Verständnis und eine rationelle Anwendung von Konstruktoren können die Qualität und Effizienz von Code verbessern.
Wir werden diese Funktion auch verwenden, um die Eigenschaften des Objekts in der Klasse mit den angegebenen Parametern zu initialisieren.
Abschließend sehen wir uns an, wie man eine Funktion in 子类
中启动对象并在两个类都有单独的构造函数时调用父类
erstellt.
Im folgenden Beispiel erstellen wir eine Klasse Student
并使用 __construct
函数为 new Student
und weisen ihre Eigenschaften zu.
__construct
函数减少了与使用函数 set_name()
Relevante Anzahl an Codes.
<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>
Ausgabe:
<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
in der Klasse mit dem PHP-Konstruktor
, um die Eigenschaften und Parameter des von uns erstellten Objekts bereitzustellen. 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>Ausgabe:
<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
verfügen
<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>
Klassenkonstrukteur. Identity
类扩展了上述代码中的 Student
类。我们使用关键字 parent:
来调用 Student
Das obige ist der detaillierte Inhalt vonKonstruktor in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!