<?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.inheritance.php',
    1 => 'H&eacute;ritage',
    2 => 'H&eacute;ritage',
  ),
  'up' => 
  array (
    0 => 'language.oop5.php',
    1 => 'Les classes et les objets',
  ),
  'prev' => 
  array (
    0 => 'language.oop5.visibility.php',
    1 => 'Visibilit&eacute;',
  ),
  'next' => 
  array (
    0 => 'language.oop5.paamayim-nekudotayim.php',
    1 => 'L\'op&eacute;rateur de r&eacute;solution de port&eacute;e (::)',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'language/oop5/inheritance.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.oop5.inheritance" class="sect1">
 <h2 class="title">Héritage</h2>
 <p class="para">
  L&#039;héritage est un des grands principes de la programmation orientée objet (POO), et
  PHP l&#039;implémente dans son modèle objet. Ce principe va affecter la manière dont
  de nombreuses classes sont en relation les unes avec les autres.
 </p>
 <p class="para">
  Par exemple, lorsqu&#039;une classe est étendue, la classe enfant hérite de toutes
  les méthodes publiques et protégées, propriétés et constantes de la classe parente.
  Tant qu&#039;une classe n&#039;écrase
  pas ces méthodes, elles conservent leur fonctionnalité d&#039;origine.
 </p>
 <p class="para">
  L&#039;héritage est très utile pour définir et abstraire certaines fonctionnalités
  communes à plusieurs classes, tout en permettant la mise en place de
  fonctionnalités supplémentaires dans les classes enfants, sans avoir à réimplémenter
  en leur sein toutes les fonctionnalités communes.
 </p>
 <p class="para">
  Les méthodes privées d&#039;une classe parente ne sont pas accessibles à la classe enfant.
  Par conséquent, les classes enfant peuvent réimplémenter une méthode privée elles-mêmes
  sans se soucier des règles d&#039;héritage normales.
  Avant PHP 8.0.0, cependant, les restrictions <code class="literal">final</code>
  et <code class="literal">static</code> étaient appliquées aux méthodes privées.
  À partir de PHP 8.0.0, l&#039;unique restriction de méthode privée qui est imposée
  est <code class="literal">private final</code> sur les constructeurs, car c&#039;est une
  manière courante pour &quot;désactiver&quot; le constructeur lors de l&#039;utilisation
  de méthodes factory statiques à la place.
 </p>
 <p class="para">
  La <a href="language.oop5.visibility.php" class="link">visibilité</a>
  des méthodes, propriétés et constantes peut être relaxée, c.-à-d. une
  méthode <code class="literal">protected</code> peut être marquée comme
  <code class="literal">public</code>, mais elles ne peuvent pas être restreintes, e.g.
  marquer une propriété <code class="literal">public</code> comme <code class="literal">private</code>.
  Une exception sont les constructeurs, pour lesquels la visibilité peut être restreinte,
  par exemple un constructeur <code class="literal">public</code> peut être annoté
  en tant que <code class="literal">private</code> dans la classe enfant.
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   À moins que l&#039;autochargement ne soit utilisé, les classes doivent être connues avant d&#039;être
   utilisées. Les classes mères doivent être définies avant l&#039;écriture d&#039;un héritage. Cette
   règle générale s&#039;applique aussi dans le cas d&#039;héritage ou d&#039;implémentation d&#039;interfaces.
  </p>
 </p></blockquote>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   Il n&#039;est pas autorisé de redéfinir une propriété en lecture-écriture avec une
   <a href="language.oop5.properties.php#language.oop5.properties.readonly-properties" class="link">propriété en lecture seule</a> ou vice versa.
   <div class="informalexample">
    <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">A </span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">int $prop</span><span style="color: #007700">;<br />}<br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br />    </span><span style="color: #FF8000">// Illégal : lecture-écriture -&gt; lecture seule<br />    </span><span style="color: #007700">public readonly </span><span style="color: #0000BB">int $prop</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </p></blockquote>

 <div class="example" id="example-1">
  <p><strong>Exemple #1 Exemple d&#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 /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Foo<br /></span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">printItem</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)<br />    {<br />        echo </span><span style="color: #DD0000">'Foo: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$string </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    }<br />    <br />    public function </span><span style="color: #0000BB">printPHP</span><span style="color: #007700">()<br />    {<br />        echo </span><span style="color: #DD0000">'PHP est super' </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    }<br />}<br /><br />class </span><span style="color: #0000BB">Bar </span><span style="color: #007700">extends </span><span style="color: #0000BB">Foo<br /></span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">printItem</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)<br />    {<br />        echo </span><span style="color: #DD0000">'Bar: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$string </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= new </span><span style="color: #0000BB">Bar</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printItem</span><span style="color: #007700">(</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">); </span><span style="color: #FF8000">// Affiche : 'Foo: baz'<br /></span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printPHP</span><span style="color: #007700">();       </span><span style="color: #FF8000">// Affiche : 'PHP est super'<br /></span><span style="color: #0000BB">$bar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printItem</span><span style="color: #007700">(</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">); </span><span style="color: #FF8000">// Affiche : 'Bar: baz'<br /></span><span style="color: #0000BB">$bar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printPHP</span><span style="color: #007700">();       </span><span style="color: #FF8000">// Affiche : 'PHP est super'<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

 </div>

 <div class="sect2" id="language.oop5.inheritance.internal-classes">
  <h3 class="title">Compatibilité des types de retour avec les classes internes</h3>

  <p class="para">
   Avant PHP 8.1, la plupart des classes ou méthodes internes ne déclaraient pas leur
   type de retour, et tout type de retour était autorisé lors de leur héritage.
  </p>

  <p class="para">
   À partir de PHP 8.1.0, la plupart des méthodes internes ont commencé à déclarer &quot;provisoirement&quot;
   leur type de retour, dans ce cas, le type de retour des méthodes doit être compatible avec la classe
   parent; Dans le cas contraire, une notification de dépréciation est émise.
   Il est à noter que l&#039;absence d&#039;une déclaration de retour explicite est également considérée comme une
   erreur de signature, et entraîne donc l&#039;avis de dépréciation.
  </p>

  <p class="para">
   Si le type de retour ne peut être déclaré pour une méthode de surcharge en raison de problèmes de
   compatibilité entre versions de PHP, un attribut <span class="classname"><a href="class.returntypewillchange.php" class="classname">ReturnTypeWillChange</a></span> peut être
   ajouté pour passer sous silence l&#039;avis de dépréciation.
  </p>

  <div class="example" id="example-2">
   <p><strong>Exemple #2 La méthode surchargée ne déclare pas de type de retour.</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">MyDateTime </span><span style="color: #007700">extends </span><span style="color: #0000BB">DateTime<br /></span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">modify</span><span style="color: #007700">(</span><span style="color: #0000BB">string $modifier</span><span style="color: #007700">) { return </span><span style="color: #0000BB">false</span><span style="color: #007700">; }<br />}<br /><br /></span><span style="color: #FF8000">// "Deprecated: Return type of MyDateTime::modify(string $modifier) should either be compatible with DateTime::modify(string $modifier): DateTime|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice" À partir de PHP 8.1.0</span></span></code></div>
   </div>

  </div>

  <div class="example" id="example-3">
   <p><strong>Exemple #3 La méthode surchargée déclare un mauvais type de retour.</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">MyDateTime </span><span style="color: #007700">extends </span><span style="color: #0000BB">DateTime<br /></span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">modify</span><span style="color: #007700">(</span><span style="color: #0000BB">string $modifier</span><span style="color: #007700">): ?</span><span style="color: #0000BB">DateTime </span><span style="color: #007700">{ return </span><span style="color: #0000BB">null</span><span style="color: #007700">; }<br />}<br /><br /></span><span style="color: #FF8000">// "Deprecated: Return type of MyDateTime::modify(string $modifier): ?DateTime should either be compatible with DateTime::modify(string $modifier): DateTime|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice" À partir de PHP 8.1.0</span></span></code></div>
   </div>

  </div>

  <div class="example" id="example-4">
   <p><strong>Exemple #4 La méthode surchargée déclare un mauvais type de retour sans notice de dépréciation.</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">MyDateTime </span><span style="color: #007700">extends </span><span style="color: #0000BB">DateTime<br /></span><span style="color: #007700">{<br />    </span><span style="color: #FF8000">/**<br />     * @return DateTime|false<br />     */<br />    </span><span style="color: #007700">#[</span><span style="color: #0000BB">\ReturnTypeWillChange</span><span style="color: #007700">]<br />    public function </span><span style="color: #0000BB">modify</span><span style="color: #007700">(</span><span style="color: #0000BB">string $modifier</span><span style="color: #007700">) { return </span><span style="color: #0000BB">false</span><span style="color: #007700">; }<br />}<br /><br /></span><span style="color: #FF8000">// Aucune notice n'est déclenchée</span></span></code></div>
   </div>

  </div>
 </div>
</div><?php manual_footer($setup); ?>