PHP Magic Constants

Magic constants are predefined constants that change based on their location in the code. Unlike regular constants, they do not have fixed values; instead, their values change depending on where they are used. Magic constants start and end with double underscores (__), except for the ClassName::class constant.


Summary of PHP Magic Constants

Magic Constant

Description

__LINE__

Current line number

__FILE__

Full path and filename of the file

__DIR__

Directory of the file

__FUNCTION__

Function name

__METHOD__

Class method name

__NAMESPACE__

Current namespace

__TRAIT__

Trait name (PHP 5.4+)

ClassName::class

Returns the name of the specified class and the name of the namespace, if any.

  1. __LINE__


  • Represents the current line number in the script.
Example
<!DOCTYPE html>
<html>
<body>
<?php
echo "This is line number " . __LINE__;
?>
</body>
</html>

Try it yourself

        2. __FILE__


  • Represents the full path and filename of the file.
  • Useful for finding the script's exact location.
Example
<!DOCTYPE html>
<html>
<body>
<?php
echo __FILE__;
?>
</body>
</html>

Try it yourself

        3. __DIR__


  • Represents the directory of the file.
Example
<!DOCTYPE html>
<html>
<body>
<?php
echo __DIR__;
?>
</body>
</html>

Try it yourself

        4. __FUNCTION__


  • Represents the function name (case-sensitive) where it is used.
  • If used outside a function, it will return an empty string.
Example
<!DOCTYPE html>
<html>
<body>
<?php
function myFunction() {
echo "The function name is " . __FUNCTION__;
}
myFunction();
?>
</body>
</html>

Try it yourself

        5. __CLASS__


  • Represents the class name (case-sensitive) where it is used.
  • If used outside a class, it returns an empty string.
  • From PHP 5.4 onward, __CLASS__ includes the namespace in its value (if applicable).
Example
<!DOCTYPE html>
<html>
<body>
<?php
class MyClass {
public function getClassName() {
return __CLASS__;
}
}
$obj = new MyClass();
echo $obj->getClassName();
?>
</body>
</html>

Try it yourself

        6. __METHOD__


  • Represents the class method name where it is used.
  • Combines the class name and the method name.
Example
<!DOCTYPE html>
<html>
<body>
<?php
class MyClass {
public function myMethod() {
echo "The method name is " . __METHOD__;
}
}
$obj = new MyClass();
$obj->myMethod();
?>
</body>
</html>

Try it yourself

        7. __NAMESPACE__


  • Represents the name of the current namespace.
  • If used outside of any namespace, it returns an empty string.
Example
<?php
namespace MyNamespace;
function myValue(){
return __NAMESPACE__;
}
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo myValue();
?>
</body>
</html>

Try it yourself

        8. __TRAIT__ (Introduced in PHP 5.4)


  • Represents the trait name where it is used.
  • Useful for traits to refer to themselves.
Example
<!DOCTYPE html>
<html>
<body>
<?php
trait MyTrait {
public function getTraitName() {
return __TRAIT__;
}
}
class MyClass {
use MyTrait;
}
$obj = new MyClass();
echo $obj->getTraitName();
?>
</body>
</html>

Try it yourself

        9. ClassName::class


  • Returns the name of the specified class and the name of the namespace, if any.
Example
<?php
namespace myArea;

class Fruits {
public function myValue(){
return Fruits::class;
}
}
?>
<!DOCTYPE html>
<html>
<body>

<?php
$kiwi = new Fruits();
echo $kiwi->myValue();
?>

</body>
</html>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.