<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.constants.magic.php',
    1 => 'Magic constants',
    2 => 'Magic constants',
  ),
  'up' => 
  array (
    0 => 'language.constants.php',
    1 => 'Constants',
  ),
  'prev' => 
  array (
    0 => 'language.constants.predefined.php',
    1 => 'Predefined constants',
  ),
  'next' => 
  array (
    0 => 'language.expressions.php',
    1 => 'Expressions',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    '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">Magic constants</h2>
   <p class="simpara">
    Magic constants are keywords that are replaced with information about
    their own location in the source code: their file and line, and the
    function, class, trait, namespace, or property hook enclosing them.
    Despite their name, they are not actual constants:
    <span class="function"><a href="function.defined.php" class="function">defined()</a></span> and <span class="function"><a href="function.constant.php" class="function">constant()</a></span> do not
    recognize them, and a constant of the same name created with
    <span class="function"><a href="function.define.php" class="function">define()</a></span> does not affect them. They are case-insensitive.
    When used outside of the relevant context (e.g. <strong><code><a href="language.constants.magic.php#constant.class">__CLASS__</a></code></strong>
    outside of a class), they are replaced with an empty string.
   </p>
   <table class="doctable table">
    <caption><strong>PHP&#039;s magic constants</strong></caption>
    
     <thead>
      <tr>
       <th>Name</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>
        The line number on which it is written.
       </td>
      </tr>

      <tr id="constant.file">
       <td><strong><code><a href="language.constants.magic.php#constant.file">__FILE__</a></code></strong></td>
       <td>
        The full path and filename of the file in which it is written,
        with symlinks resolved.
       </td>
      </tr>

      <tr id="constant.dir">
       <td><strong><code><a href="language.constants.magic.php#constant.dir">__DIR__</a></code></strong></td>
       <td>
        The directory of the file, equivalent to
        <code class="literal">dirname(__FILE__)</code>. It has no trailing slash
        unless it is the root directory.
       </td>
      </tr>

      <tr id="constant.function">
       <td><strong><code><a href="language.constants.magic.php#constant.function">__FUNCTION__</a></code></strong></td>
       <td>
        The name of the function in which it is written, including its namespace
        (e.g. <code class="literal">Foo\bar</code>).
        Inside a method, only the method name.
        For anonymous functions, a name generated by the engine that includes
        where the closure was defined (as of PHP 8.4.0; previously
        <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>
        The name of the class in which it is written, including its namespace
        (e.g. <code class="literal">Foo\Bar</code>).
        Inside a trait, the name of the class using the trait.
        For <a href="language.oop5.anonymous.php" class="link">anonymous classes</a>,
        a name generated by the engine.
        To get the name of the class a method was called on, use
        <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>
        The trait name, including its namespace (e.g. <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>
        The class and method name of the method in which it is written
        (e.g. <code class="literal">Foo\Bar::baz</code>).
        Inside a trait, the trait name is used instead of the class name.
        Outside of a method, the same as <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>
        The name of the property, when used inside a
        <a href="language.oop5.property-hooks.php" class="link">property hook</a>.
        Available as of 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>
        The name of the current namespace.
       </td>
      </tr>

     </tbody>
    
   </table>


   <div class="example" id="example-1">
    <p><strong>Example #1 Using magic constants</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>The above example will output:</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">See Also</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); ?>