techmore.in

PHP - Magic Constants

In PHP, magic constants are predefined constants that change depending on where they are used. They provide useful information about the current file, line number, function, class, method, namespace, and trait. Here’s a detailed look at each of the magic constants available in PHP:

List of Magic Constants

  1. __LINE__
  2. __FILE__
  3. __DIR__
  4. __FUNCTION__
  5. __CLASS__
  6. __METHOD__
  7. __NAMESPACE__
  8. __TRAIT__

Detailed Explanation and Examples

1. __LINE__

Represents the current line number in the script.

Example:

php
<?php echo "This is line " . __LINE__ . "\n"; ?>

Output:

arduino
This is line 3

2. __FILE__

Represents the full path and filename of the file.

Example:

php
<?php echo "The file path is " . __FILE__ . "\n"; ?>

Output:

lua
The file path is /path/to/your/script.php

3. __DIR__

Represents the directory of the file. Equivalent to dirname(__FILE__).

Example:

php
<?php echo "The directory is " . __DIR__ . "\n"; ?>

Output:

vbnet
The directory is /path/to/your

4. __FUNCTION__

Represents the function name. Returns the name of the function in which it is used.

Example:

php
<?php function myFunction() { echo "The function name is " . __FUNCTION__ . "\n"; } myFunction(); ?>

Output:

vbnet
The function name is myFunction

5. __CLASS__

Represents the class name. Returns the name of the class in which it is used. This constant is case-sensitive.

Example:

php
<?php class MyClass { public function showClassName() { echo "The class name is " . __CLASS__ . "\n"; } } $obj = new MyClass(); $obj->showClassName(); ?>

Output:

vbnet
The class name is MyClass

6. __METHOD__

Represents the class method name. Returns the name of the class method in which it is used. This constant is case-sensitive.

Example:

php
<?php class MyClass { public function myMethod() { echo "The method name is " . __METHOD__ . "\n"; } } $obj = new MyClass(); $obj->myMethod(); ?>

Output:

vbnet
The method name is MyClass::myMethod

7. __NAMESPACE__

Represents the current namespace name. This constant is case-sensitive.

Example:

php
<?php namespace MyNamespace; echo "The namespace is " . __NAMESPACE__ . "\n"; ?>

Output:

vbnet
The namespace is MyNamespace

8. __TRAIT__

Represents the trait name. Returns the name of the trait in which it is used. This constant is case-sensitive.

Example:

php
<?php trait MyTrait { public function showTraitName() { echo "The trait name is " . __TRAIT__ . "\n"; } } class MyClass { use MyTrait; } $obj = new MyClass(); $obj->showTraitName(); ?>

Output:

csharp
The trait name is MyTrait

Summary

Magic constants in PHP are powerful tools that provide context-specific information useful for debugging and informative purposes. Here’s a quick reference:

  • __LINE__: Current line number.
  • __FILE__: Full path and filename of the file.
  • __DIR__: Directory of the file.
  • __FUNCTION__: Function name.
  • __CLASS__: Class name.
  • __METHOD__: Class method name.
  • __NAMESPACE__: Current namespace name.
  • __TRAIT__: Trait name.

These constants help make your code more introspective and self-aware, especially useful in logging, debugging, and error reporting scenarios.