- PHP Basics
- PHP - Introduction
- PHP - Install
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo or Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP – Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Advanced
- PHP - Operators
- PHP - Control Statements
- PHP - Arrays
- PHP - Functions
- PHP - Superglobals
- PHP - File Handling
- PHP- Object Oriented Programming
- PHP - AJAX
- PHP - Web Development
- PHP - Enums
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
__LINE____FILE____DIR____FUNCTION____CLASS____METHOD____NAMESPACE____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:
arduinoThis 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:
luaThe 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:
vbnetThe 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:
vbnetThe 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:
vbnetThe 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:
vbnetThe 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:
vbnetThe 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:
csharpThe 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.