Constructeur en PHP

PHPz
PHPzavant
2024-02-28 08:22:57478parcourir

L'éditeur PHP Xiaoxin vous donnera une analyse détaillée du constructeur en PHP. Un constructeur est une méthode qui est automatiquement appelée lorsqu'un objet est instancié pour initialiser les propriétés de l'objet. Grâce au constructeur, vous pouvez transmettre des paramètres et attribuer des valeurs aux propriétés lors de la création d'un objet, améliorant ainsi la lisibilité et la réutilisabilité du code. Le nom du constructeur est le même que le nom de la classe et il n'est pas nécessaire de l'appeler manuellement, ce qui facilite la gestion et la maintenance unifiées du code. En PHP, les constructeurs constituent une partie importante de la programmation orientée objet. Une compréhension approfondie et une application rationnelle des constructeurs peuvent améliorer la qualité et l'efficacité du code.

Nous utiliserons également cette fonction pour initialiser les propriétés de l'objet dans la classe avec les paramètres donnés.

Enfin, nous verrons comment construire une fonction dans 子类中启动对象并在两个类都有单独的构造函数时调用父类.


Utilisez le constructeur php pour initialiser les propriétés des objets de la classe

Dans l'exemple suivant, nous allons créer une classe Student 并使用 __construct 函数为 new Student et attribuer ses propriétés.

__construct 函数减少了与使用函数 set_name() Nombre de codes pertinent.

<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>

Résultat :

<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>

Initialisez les propriétés de Object with Parameters

dans la classe à l'aide du constructeur PHP

Dans l'exemple de code ci-dessous, nous créons une fonction de classe Military 并使用 __construct pour fournir les propriétés et les paramètres de l'objet que nous créons.

<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 &#39;Military&#39;
</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>

Résultat :

<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>

En PHP, démarrez un objet dans la classe enfant et appelez le constructeur de la classe parent Individual lorsque les deux classes ont un constructeur

<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>

Résultat :

<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 Constructeur de classe.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer
Article précédent:Fonction PHP Base64_decodeArticle suivant:Fonction PHP Base64_decode