Magical Constants

PHP provides some predefined, compile-time constants.

test.php:
<?php   // test.php
namespace test;
class C{
   public $v;
   function print_names(){
      echo __LINE__."<br />";
      echo __FILE__."<br />";
      echo __DIR__."<br />";
      echo __CLASS__."<br />";
      echo __METHOD__."<br />";
      echo __FUNCTION__."<br />";
      echo __NAMESPACE__."<br />"; // NULL
      // echo __TRAIT__;
   }
}
$o= new C;
$o->print_names();
?>

<html>   // intro.php
<head></head>
<body><?php
include "test.php";
?></body>
</html>

// intro.php 6
D:\xampp\htdocs\contents\processed\en\php\test.php
D:\xampp\htdocs\contents\processed\en\php
test\C
test\C::print_names
print_names
test
Constant expressions (the values of const declarations, class constants, and default parameter values) used to accept only a limited set of operations. Since PHP 8.5, they may also contain a cast and a closure – including first-class callable syntax like strlen(...) – both of which previously caused a “Constant expression contains invalid operations” error:

<?php
const T1 = (int) 0.3;
echo T1, "\n";

class C {
  const HANDLER = strlen(...);
}

$fn = C::HANDLER;
echo $fn("hello"), "\n";
echo get_class($fn), "\n";
?>

0 5 Closure