<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.oop5.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'language.oop5.decon.php',
    1 => 'Constructeurs et destructeurs',
    2 => 'Constructeurs et destructeurs',
  ),
  'up' => 
  array (
    0 => 'language.oop5.php',
    1 => 'Les classes et les objets',
  ),
  'prev' => 
  array (
    0 => 'language.oop5.autoload.php',
    1 => 'Auto-chargement de classes',
  ),
  'next' => 
  array (
    0 => 'language.oop5.visibility.php',
    1 => 'Visibilit&eacute;',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'language/oop5/decon.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.oop5.decon" class="sect1">
  <h2 class="title">Constructeurs et destructeurs</h2>

  <div class="sect2" id="language.oop5.decon.constructor">
   <h3 class="title">Constructeur</h3>
   <div class="methodsynopsis dc-description" id="object.construct">
    <span class="methodname"><strong>__construct</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">...$values</code><span class="initializer"> = &quot;&quot;</span></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

   <p class="para">
    PHP permet aux développeurs de déclarer des constructeurs pour
    les classes. Les classes qui possèdent une méthode constructeur
    appellent cette méthode à chaque création d&#039;une nouvelle instance
    de l&#039;objet, ce qui est intéressant pour toutes les initialisations
    dont l&#039;objet a besoin avant d&#039;être utilisé.
   </p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     Les constructeurs parents ne sont pas appelés implicitement
     si la classe enfant définit un constructeur. Pour
     utiliser un constructeur parent, il est nécessaire de faire
     appel à <span class="function"><strong>parent::__construct()</strong></span> depuis le constructeur enfant.
     Si l&#039;enfant ne définit pas un constructeur alors, il peut être hérité
     de la classe parente, exactement de la même façon qu&#039;une méthode le serait
     (si elle n&#039;a pas été déclarée comme privée).
    </span>
   </p></blockquote>
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Constructeur lors de l&#039;héritage</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">BaseClass </span><span style="color: #007700">{<br />    function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">() {<br />        print </span><span style="color: #DD0000">"Dans le constructeur de BaseClass\n"</span><span style="color: #007700">;<br />    }<br />}<br /><br />class </span><span style="color: #0000BB">SubClass </span><span style="color: #007700">extends </span><span style="color: #0000BB">BaseClass </span><span style="color: #007700">{<br />    function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">() {<br />        </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">();<br />        print </span><span style="color: #DD0000">"Dans le constructeur de SubClass\n"</span><span style="color: #007700">;<br />    }<br />}<br /><br />class </span><span style="color: #0000BB">OtherSubClass </span><span style="color: #007700">extends </span><span style="color: #0000BB">BaseClass </span><span style="color: #007700">{<br />    </span><span style="color: #FF8000">// Constructeur hérité de BaseClass<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">// Dans le constructeur de BaseClass<br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">BaseClass</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Dans le constructeur de BaseClass<br />// Dans le constructeur de SubClass<br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">SubClass</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Dans le constructeur de BaseClass<br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">OtherSubClass</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <p class="para">
    À la différence des autres méthodes, <a href="language.oop5.decon.php#object.construct" class="link">__construct()</a>
    est exclue des <a href="language.oop5.basic.php#language.oop.lsp" class="link">règles de compatibilités des signatures</a>
    usuelles quand elle est étendue.
   </p>
   <p class="para">
    Les constructeurs sont des méthodes ordinaires qui sont appelées lors de
    l&#039;instanciation de leur objet correspondant. Par conséquent, ils peuvent
    définir un nombre arbitraire d&#039;arguments, qui peuvent être requis, avoir un type,
    et peuvent avoir une valeur par défaut. Les arguments d&#039;un constructeur sont appelés
    en plaçant les arguments dans des parenthèses après le nom de la classe.
   </p>
   <div class="example" id="example-2">
    <p><strong>Exemple #2 Utiliser les arguments d&#039;un constructeur</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Point </span><span style="color: #007700">{<br />    protected </span><span style="color: #0000BB">int $x</span><span style="color: #007700">;<br />    protected </span><span style="color: #0000BB">int $y</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">int $x</span><span style="color: #007700">, </span><span style="color: #0000BB">int $y </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">x </span><span style="color: #007700">= </span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">y </span><span style="color: #007700">= </span><span style="color: #0000BB">$y</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #FF8000">// Passe les deux paramètres.<br /></span><span style="color: #0000BB">$p1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">Point</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Passe uniquement le paramètre requis. $y prendra sa valeur par défaut de 0.<br /></span><span style="color: #0000BB">$p2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">Point</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Avec les arguments nommés (à partir de PHP 8.0) :<br /></span><span style="color: #0000BB">$p3 </span><span style="color: #007700">= new </span><span style="color: #0000BB">Point</span><span style="color: #007700">(</span><span style="color: #0000BB">y</span><span style="color: #007700">: </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">x</span><span style="color: #007700">: </span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <p class="para">
    Si une classe n&#039;a pas de constructeur, ou que le constructeur n&#039;a pas d&#039;arguments requis,
    les parenthèses peuvent être omises.
   </p>
   <div class="sect3">
    <h4 class="title">Style ancien des constructeurs</h4>
    <p class="para">
     Avant PHP 8.0.0, les classes dans le nom d&#039;espace global interpréteront une
     méthode qui a le même nom que la classe comme un constructeur de style ancien.
     Cette syntaxe est obsolète, et résultera en une erreur <strong><code><a href="errorfunc.constants.php#constant.e-deprecated">E_DEPRECATED</a></code></strong>
     mais appellera quand même cette fonction comme un constructeur.
     Si <a href="language.oop5.decon.php#object.construct" class="link">__construct()</a> et une méthode du même nom
     sont définies, <a href="language.oop5.decon.php#object.construct" class="link">__construct()</a> sera appelé.
    </p>
    <p class="para">
     Les classes dans les espaces de nom, ou toute classe à partir de PHP 8.0.0,
     une méthode du même nom que la classe n&#039;a jamais de signification particulière.
    </p>
    <p class="para">
     Toujours utiliser <a href="language.oop5.decon.php#object.construct" class="link">__construct()</a> dans du nouveau code.
    </p>
   </div>
   <div class="sect3" id="language.oop5.decon.constructor.promotion">
    <h4 class="title">Promotion du Constructeur</h4>
    <p class="para">
     À partir de PHP 8.0.0, les paramètres du constructeur peuvent être promus pour
     correspondre à une propriété de l&#039;objet. Il est très courant pour les paramètres
     d&#039;un constructeur d&#039;être assignés à une propriété sans effectuer d&#039;opérations dessus.
     La promotion du constructeur fournit un raccourci pour ce cas d&#039;utilisation.
     L&#039;exemple ci-dessus peut être réécrit de la façon suivante.
    </p>
    <div class="example" id="example-3">
     <p><strong>Exemple #3 Utilisant la promotion de propriété de constructeur</strong></p>
     <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Point </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(protected </span><span style="color: #0000BB">int $x</span><span style="color: #007700">, protected </span><span style="color: #0000BB">int $y </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br />    }<br />}</span></span></code></div>
     </div>

    </div>
    <p class="para">
     Quand un argument de constructeur inclut un modificateur, PHP l&#039;interprétera
     comme une propriété d&#039;objet et un argument du constructeur, et assigne la valeur de l&#039;argument
     à la propriété. Le corps du constructeur peut être alors vide ou peut contenir d&#039;autres
     déclarations. Toutes les déclarations additionnelles seront exécutées après que la valeur de
     l&#039;argument a été assignée à sa propriété correspondante.
    </p>
    <p class="para">
     Tous les arguments ne doivent pas être promus. Il est possible de mélanger et assortir les
     arguments promus et non-promus, dans n&#039;importe quel ordre. Les arguments promus n&#039;ont aucun impact
     sur du code appelant le constructeur.
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      Utiliser un <a href="language.oop5.visibility.php" class="link">modificateur de visibilité</a>
      (<code class="literal">public</code>, <code class="literal">protected</code> ou <code class="literal">private</code>) est
      la manière la plus probable d&#039;appliquer la promotion de propriété, mais tout autre modificateur
      unique (comme <code class="literal">readonly</code>) aura le même effet.
     </p>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      Les propriétés d&#039;objet ne peuvent être typées <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> à cause des ambiguïtés du moteur
      que ceci introduirait. Ainsi, les arguments promus, ne peuvent pas non plus être typés
      <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span>. Cependant, toute autre
      <a href="language.types.declarations.php" class="link">déclaration de type</a> est permise.
     </p>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      Comme les propriétés promues sont à la fois désucrées à une propriété
      et aussi comme le paramètre d&#039;une fonction, toutes les restrictions de
      nommage pour les propriétés et les paramètres s&#039;appliquent.
     </p>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      Les <a href="language.attributes.php" class="link">attributs</a> placés sur un
      argument de constructeur promu seront reproduits à la fois sur la
      propriété et sur l&#039;argument. Les valeurs par défaut sur un argument de
      constructeur promu ne seront répliquées que sur l&#039;argument et non sur la propriété.
     </p>
    </p></blockquote>
   </div>

   <div class="sect3" id="language.oop5.decon.constructor.new">
    <h4 class="title">New in initializers</h4>
    <p class="para">
     À partir de PHP 8.1.0, les objets peuvent être utilisés comme valeur par défaut pour les paramètres,
     variables statiques, constantes globales et les arguments d&#039;attributs.
     Les objets peuvent être désormais passés à <span class="function"><a href="function.define.php" class="function">define()</a></span>.
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      L&#039;utilisation d&#039;un nom de classe dynamique ou non-string ou une class anonyme n&#039;est pas permise.
      L&#039;utilisation du déballage d&#039;arguments n&#039;est pas permise.
      L&#039;utilisation d&#039;expressions non supportées en tant qu&#039;argument n&#039;est pas permise.
     </p>
    </p></blockquote>
    <div class="example" id="example-4">
     <p><strong>Exemple #4 Utilisation de new dans des initialiseurs</strong></p>
     <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// Tous autorisés :<br /></span><span style="color: #007700">static </span><span style="color: #0000BB">$x </span><span style="color: #007700">= new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">;<br /><br />const </span><span style="color: #0000BB">C </span><span style="color: #007700">= new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">;<br /><br />function </span><span style="color: #0000BB">test</span><span style="color: #007700">(</span><span style="color: #0000BB">$param </span><span style="color: #007700">= new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">) {}<br /><br />#[</span><span style="color: #0000BB">AnAttribute</span><span style="color: #007700">(new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">)]<br />class </span><span style="color: #0000BB">Test </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(<br />        public </span><span style="color: #0000BB">$prop </span><span style="color: #007700">= new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">,<br />    ) {}<br />}<br /><br /></span><span style="color: #FF8000">// Aucun n'est autorisé (erreur de compilation) :<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">test</span><span style="color: #007700">(<br />    </span><span style="color: #0000BB">$a </span><span style="color: #007700">= new (</span><span style="color: #0000BB">CLASS_NAME_CONSTANT</span><span style="color: #007700">)(), </span><span style="color: #FF8000">// nom de classe dynamique<br />    </span><span style="color: #0000BB">$b </span><span style="color: #007700">= new class {}, </span><span style="color: #FF8000">// classe anonyme<br />    </span><span style="color: #0000BB">$c </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">(...[]), </span><span style="color: #FF8000">// déballage d'arguments<br />    </span><span style="color: #0000BB">$d </span><span style="color: #007700">= new </span><span style="color: #0000BB">B</span><span style="color: #007700">(</span><span style="color: #0000BB">$abc</span><span style="color: #007700">), </span><span style="color: #FF8000">// expression de constante non supportée<br /></span><span style="color: #007700">) {}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </div>

   <div class="sect3" id="language.oop5.decon.constructor.static">
    <h4 class="title">Méthode de création statique</h4>
    <p class="para">
     PHP supporte uniquement un constructeur unique par classe. Cependant, dans
     certains cas il peut être désirable de permettre à un objet d&#039;être construit
     de manière différente avec des entrées différentes.
     La façon recommandée de faire ceci est en utilisant des méthodes statiques
     comme une enveloppe du constructeur.
    </p>
    <div class="example" id="example-5">
     <p><strong>Exemple #5 Utilisant la création via une méthode statique</strong></p>
     <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$some_json_string </span><span style="color: #007700">= </span><span style="color: #DD0000">'{ "id": 1004, "name": "Elephpant" }'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$some_xml_string </span><span style="color: #007700">= </span><span style="color: #DD0000">"&lt;animal&gt;&lt;id&gt;1005&lt;/id&gt;&lt;name&gt;Elephpant&lt;/name&gt;&lt;/animal&gt;"</span><span style="color: #007700">;<br /><br />class </span><span style="color: #0000BB">Product </span><span style="color: #007700">{<br /><br />    private ?</span><span style="color: #0000BB">int $id</span><span style="color: #007700">;<br />    private ?</span><span style="color: #0000BB">string $name</span><span style="color: #007700">;<br /><br />    private function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(?</span><span style="color: #0000BB">int $id </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">, ?</span><span style="color: #0000BB">string $name </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id </span><span style="color: #007700">= </span><span style="color: #0000BB">$id</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br />    }<br /><br />    public static function </span><span style="color: #0000BB">fromBasicData</span><span style="color: #007700">(</span><span style="color: #0000BB">int $id</span><span style="color: #007700">, </span><span style="color: #0000BB">string $name</span><span style="color: #007700">): static {<br />        </span><span style="color: #0000BB">$new </span><span style="color: #007700">= new static(</span><span style="color: #0000BB">$id</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br />        return </span><span style="color: #0000BB">$new</span><span style="color: #007700">;<br />    }<br /><br />    public static function </span><span style="color: #0000BB">fromJson</span><span style="color: #007700">(</span><span style="color: #0000BB">string $json</span><span style="color: #007700">): static {<br />        </span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br />        return new static(</span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">], </span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]);<br />    }<br /><br />    public static function </span><span style="color: #0000BB">fromXml</span><span style="color: #007700">(</span><span style="color: #0000BB">string $xml</span><span style="color: #007700">): static {<br />        </span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">simplexml_load_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">);<br />        </span><span style="color: #0000BB">$new </span><span style="color: #007700">= new static();<br />        </span><span style="color: #0000BB">$new</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id </span><span style="color: #007700">= (int) </span><span style="color: #0000BB">$data</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$new</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #0000BB">$data</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">;<br />        return </span><span style="color: #0000BB">$new</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$p1 </span><span style="color: #007700">= </span><span style="color: #0000BB">Product</span><span style="color: #007700">::</span><span style="color: #0000BB">fromBasicData</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #DD0000">'Widget'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$p2 </span><span style="color: #007700">= </span><span style="color: #0000BB">Product</span><span style="color: #007700">::</span><span style="color: #0000BB">fromJson</span><span style="color: #007700">(</span><span style="color: #0000BB">$some_json_string</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$p3 </span><span style="color: #007700">= </span><span style="color: #0000BB">Product</span><span style="color: #007700">::</span><span style="color: #0000BB">fromXml</span><span style="color: #007700">(</span><span style="color: #0000BB">$some_xml_string</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$p1</span><span style="color: #007700">, </span><span style="color: #0000BB">$p2</span><span style="color: #007700">, </span><span style="color: #0000BB">$p3</span><span style="color: #007700">);</span></span></code></div>
     </div>

    </div>
    <p class="para">
     Le constructeur peut être rendu privé ou protégé pour empêcher son appel
     depuis l&#039;extérieur. Dans ce cas, seule une méthode statique sera capable
     d&#039;instancier la classe. Puisqu&#039;elles sont dans la même définition de classe,
     elles ont accès aux méthodes privées, même dans une instance différente de
     l&#039;objet. Un constructeur privé est optionnel et peut ou non avoir du sens
     en fonction du cas d&#039;utilisation.
    </p>
    <p class="para">
     Les trois méthodes statiques publiques démontrent alors des manières différentes
     d&#039;instancier l&#039;objet.
    </p>
    <ul class="simplelist">
     <li><code class="code">fromBasicData()</code> prend les paramètres exacts qui sont nécessaires,
      puis crée l'objet en appelant le constructeur puis retournant le résultat.</li>
     <li><code class="code">fromJson()</code> accepte une chaîne de caractères JSON et fait
      du pré-traitement sur lui-même pour se convertir dans le format désiré par
      le constructeur. Elle retourne alors le nouvel objet.</li>
     <li>
      <code class="code">fromXml()</code> accepte une chaîne de caractères XML, fait du pré-traitement,
      puis crée un objet brut. Le constructeur est appelé, mais comme tous les
      paramètres sont optionnels la méthode les ignore. Elle assigne alors les
      valeurs aux propriétés de l'objet directement avant de retourner le résultat.
     </li>
    </ul>
    <p class="para">
     Dans les trois cas, le mot-clé <code class="code">static</code> est traduit en le nom de
     la classe dont le code y est. Dans ce cas, <code class="code">Product</code>.
    </p>
   </div>
  </div>

  <div class="sect2" id="language.oop5.decon.destructor">
   <h3 class="title">Destructeur</h3>
   <div class="methodsynopsis dc-description" id="object.destruct">
    <span class="methodname"><strong>__destruct</strong></span>(): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

   <p class="para">
    PHP possède un concept de destructeur similaire à celui d&#039;autres langages
    orientés objet, comme le <code class="literal">C++</code>. La méthode destructeur est appelée
    dès qu&#039;il n&#039;y a plus de référence sur un objet donné, ou dans n&#039;importe quel
    ordre pendant la séquence d&#039;arrêt.
   </p>
   <div class="example" id="example-6">
    <p><strong>Exemple #6 Exemple avec un Destructeur</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyDestructableClass <br /></span><span style="color: #007700">{<br />    function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">() {<br />        print </span><span style="color: #DD0000">"Dans le constructeur\n"</span><span style="color: #007700">;<br />    }<br /><br />    function </span><span style="color: #0000BB">__destruct</span><span style="color: #007700">() {<br />        print </span><span style="color: #DD0000">"Destruction de " </span><span style="color: #007700">. </span><span style="color: #0000BB">__CLASS__ </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyDestructableClass</span><span style="color: #007700">();</span></span></code></div>
    </div>

   </div>
   <p class="para">
    Tout comme le constructeur, le destructeur parent ne sera pas appelé
    implicitement par le moteur. Pour exécuter le destructeur parent, il
    faut appeler explicitement la fonction
    <span class="function"><strong>parent::__destruct()</strong></span> dans le corps du destructeur.
    Tout comme les constructeurs, une classe enfant peut hériter du
    destructeur du parent s&#039;il n&#039;en implémente pas un lui-même.
   </p>
   <p class="para">
    Le destructeur sera appelé même si l&#039;exécution du script est stoppée
    en utilisant la fonction <span class="function"><a href="function.exit.php" class="function">exit()</a></span>.
    Appeler la fonction <span class="function"><a href="function.exit.php" class="function">exit()</a></span>
    dans un destructeur empêchera l&#039;exécution des routines d&#039;arrêt restantes.
   </p>
   <p class="para">
    Si un destructeur crée de nouvelles références à son objet, il ne sera pas
    appelé une seconde fois lorsque le compteur de références atteint de
    nouveau zéro ou durant la séquence d&#039;arrêt.
   </p>
   <p class="para">
    À partir de PHP 8.4.0, lorsque la
    <a href="features.gc.collecting-cycles.php" class="link">collecte des cycles</a>
    se produit pendant l&#039;exécution d&#039;une
    <a href="language.fibers.php" class="link">fibre</a>, les destructeurs des objets
    programmés pour la collecte sont exécutés dans une fibre distincte, appelée
    <code class="literal">gc_destructor_fiber</code>.
    Si cette fibre est suspendue, une nouvelle fibre sera créée pour exécuter
    les destructeurs restants.
    La précédente <code class="literal">gc_destructor_fiber</code> ne sera plus référencée
    par le ramasse-miettes et pourra être collectée si elle n&#039;est pas référencée
    ailleurs.
    Les objets dont le destructeur est suspendu ne seront pas collectés tant
    que le destructeur n&#039;a pas terminé ou que la fibre elle-même n&#039;est pas
    collectée.
   </p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Les destructeurs appelés durant l&#039;arrêt du script sont dans une situation
     où les en-têtes HTTP ont déjà été envoyés.
     Le dossier de travail dans la phase d&#039;arrêt du script
     peut être différent avec certaines SAPIs (par exemple Apache).
    </p>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Tenter de lancer une exception depuis un destructeur (appelé à la fin du script)
     entraîne une erreur fatale.
    </p>
   </p></blockquote>
  </div>
 </div><?php manual_footer($setup); ?>