<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.constants.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'language.constants.magic.php',
    1 => 'Constantes magiques',
    2 => 'Constantes magiques',
  ),
  'up' => 
  array (
    0 => 'language.constants.php',
    1 => 'Les constantes',
  ),
  'prev' => 
  array (
    0 => 'language.constants.predefined.php',
    1 => 'Constantes pr&eacute;d&eacute;finies',
  ),
  'next' => 
  array (
    0 => 'language.expressions.php',
    1 => 'Les expressions',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'language/constants.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.constants.magic" class="sect1">
  <h2 class="title">Constantes magiques</h2>
  <p class="simpara">
   Les constantes magiques sont des mots-clés qui sont remplacés par des
   informations sur leur propre emplacement dans le code source : leur
   fichier et leur ligne, ainsi que la fonction, la classe, le trait,
   l&#039;espace de noms ou le hook de propriété qui les entourent.
   Malgré leur nom, ce ne sont pas de vraies constantes :
   <span class="function"><a href="function.defined.php" class="function">defined()</a></span> et <span class="function"><a href="function.constant.php" class="function">constant()</a></span> ne les
   reconnaissent pas, et une constante du même nom créée avec
   <span class="function"><a href="function.define.php" class="function">define()</a></span> n&#039;a aucun effet sur elles. Elles sont
   insensibles à la casse. Lorsqu&#039;elles sont utilisées en dehors du
   contexte auquel elles se rapportent (par exemple
   <strong><code><a href="language.constants.magic.php#constant.class">__CLASS__</a></code></strong> en dehors d&#039;une classe), elles sont
   remplacées par une chaîne vide.
  </p>
  <table class="doctable table">
   <caption><strong>Les constantes magiques de PHP</strong></caption>
   
    <thead>
     <tr>
      <th>Nom</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr id="constant.line">
      <td><strong><code><a href="language.constants.magic.php#constant.line">__LINE__</a></code></strong></td>
      <td>
       La ligne sur laquelle elle est écrite.
      </td>
     </tr>

     <tr id="constant.file">
      <td><strong><code><a href="language.constants.magic.php#constant.file">__FILE__</a></code></strong></td>
      <td>
       Le chemin complet et le nom du fichier dans lequel elle est écrite,
       avec les liens symboliques résolus.
      </td>
     </tr>

     <tr id="constant.dir">
      <td><strong><code><a href="language.constants.magic.php#constant.dir">__DIR__</a></code></strong></td>
      <td>
       Le dossier du fichier, équivalent à
       <code class="literal">dirname(__FILE__)</code>. Il ne contient pas de slash
       final, sauf s&#039;il s&#039;agit du dossier racine.
      </td>
     </tr>

     <tr id="constant.function">
      <td><strong><code><a href="language.constants.magic.php#constant.function">__FUNCTION__</a></code></strong></td>
      <td>
       Le nom de la fonction dans laquelle elle est écrite, incluant son
       espace de noms (par exemple <code class="literal">Foo\bar</code>).
       À l&#039;intérieur d&#039;une méthode, seul le nom de la méthode.
       Pour les fonctions anonymes, un nom généré par le moteur qui inclut
       l&#039;endroit où la fermeture a été définie (à partir de PHP 8.4.0 ;
       auparavant <code class="literal">{closure}</code>).
      </td>
     </tr>

     <tr id="constant.class">
      <td><strong><code><a href="language.constants.magic.php#constant.class">__CLASS__</a></code></strong></td>
      <td>
       Le nom de la classe dans laquelle elle est écrite, incluant son
       espace de noms (par exemple <code class="literal">Foo\Bar</code>).
       À l&#039;intérieur d&#039;un trait, le nom de la classe qui utilise le trait.
       Pour les <a href="language.oop5.anonymous.php" class="link">classes
       anonymes</a>, un nom généré par le moteur.
       Pour obtenir le nom de la classe sur laquelle une méthode a été
       appelée, utiliser
       <a href="language.oop5.late-static-bindings.php" class="link"><code class="literal">static::class</code></a>.
      </td>
     </tr>

     <tr id="constant.trait">
      <td><strong><code><a href="language.constants.magic.php#constant.trait">__TRAIT__</a></code></strong></td>
      <td>
       Le nom du trait, incluant son espace de noms (par exemple
       <code class="literal">Foo\Bar</code>).
      </td>
     </tr>

     <tr id="constant.method">
      <td><strong><code><a href="language.constants.magic.php#constant.method">__METHOD__</a></code></strong></td>
      <td>
       Le nom de la classe et de la méthode dans laquelle elle est écrite
       (par exemple <code class="literal">Foo\Bar::baz</code>).
       À l&#039;intérieur d&#039;un trait, le nom du trait est utilisé à la place du
       nom de la classe.
       En dehors d&#039;une méthode, identique à <strong><code><a href="language.constants.magic.php#constant.function">__FUNCTION__</a></code></strong>.
      </td>
     </tr>

     <tr id="constant.property">
      <td><strong><code><a href="language.constants.magic.php#constant.property">__PROPERTY__</a></code></strong></td>
      <td>
       Le nom de la propriété, lorsqu&#039;elle est utilisée à l&#039;intérieur d&#039;un
       <a href="language.oop5.property-hooks.php" class="link">hook de propriété</a>.
       À partir de PHP 8.4.0.
      </td>
     </tr>

     <tr id="constant.namespace">
      <td><strong><code><a href="language.constants.magic.php#constant.namespace">__NAMESPACE__</a></code></strong></td>
      <td>
       Le nom de l&#039;espace de noms courant.
      </td>
     </tr>

    </tbody>
   
  </table>


  <div class="example" id="example-1">
   <p><strong>Exemple #1 Utilisation des constantes magiques</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">namespace </span><span style="color: #0000BB">App</span><span style="color: #007700">;

function </span><span style="color: #0000BB">helper</span><span style="color: #007700">()
{
    echo </span><span style="color: #0000BB">__FUNCTION__</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
    echo </span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
}

trait </span><span style="color: #0000BB">Greets
</span><span style="color: #007700">{
    public function </span><span style="color: #0000BB">greet</span><span style="color: #007700">()
    {
        echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        echo </span><span style="color: #0000BB">__TRAIT__</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        echo </span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        echo </span><span style="color: #0000BB">__FUNCTION__</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
    }
}

class </span><span style="color: #0000BB">Greeter
</span><span style="color: #007700">{
    use </span><span style="color: #0000BB">Greets</span><span style="color: #007700">;
}

</span><span style="color: #0000BB">helper</span><span style="color: #007700">();
(new </span><span style="color: #0000BB">Greeter</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">greet</span><span style="color: #007700">();
echo </span><span style="color: #0000BB">__NAMESPACE__</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
App\helper
App\helper
App\Greeter
App\Greets
App\Greets::greet
greet
App
</pre></div>
   </div>
  </div>

  <div class="sect2">
   <h3 class="title">Voir aussi</h3>
   <p class="para">
    <ul class="simplelist">
     <li><a href="language.oop5.basic.php#language.oop5.basic.class.class" class="link">::class</a></li>
     <li><span class="function"><a href="function.get-class.php" class="function">get_class()</a></span></li>
     <li><span class="function"><a href="function.get-called-class.php" class="function">get_called_class()</a></span></li>
     <li><span class="function"><a href="function.dirname.php" class="function">dirname()</a></span></li>
    </ul>
   </p>
  </div>

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