Magic Constants PHP
- PHP Tutorial
- What is PHP
- Install PHP
- PHP Example
- PHP Echo
- PHP Print
- PHP Variables
- PHP $ and $$ Variables
- PHP Constants
- Magic Constants
- PHP Data Types
- PHP Operators
- PHP Comments
- PHP Boolean
- PHP is_bool() function
- PHP Integer
- PHP is_int() function
- PHP Float
- PHP is_float Function
- PHP Compound Types
- PHP Special Types
- PHP is_null() function
- PHP If Else
- PHP Switch
- PHP For Loop
- PHP While Loop
- PHP do while loop
- PHP Break
- PHP Functions
- PHP Parameterized Function
- PHP Call By Value
- PHP Call By Reference
- PHP Default Argument Values Function
- PHP Variable Length Argument Function
- PHP Recursive Function
- PHP Arrays
- PHP Indexed Array
- PHP Associative Array
- PHP Multidimensional Array
- PHP Array Functions
- PHP String
- PHP String Functions
- PHP Math functions
- PHP Form Handling | GET and POST method
- PHP Include File
- PHP Cookie
- PHP Session
- PHP File Handling
- PHP Open File
- PHP Read File
- PHP Write File
- PHP Append to File
- PHP Delete File
- PHP File Upload
- PHP Download File
- PHP Mail
- PHP MySQL Connect
- PHP MySQL Create Database
- PHP MySQL Create Table Example
- PHP MySQL Insert Record
- PHP Prepared Statements
- PHP MySQL Update Record
- PHP MySQL Delete Record
- PHP MySQL Select
- PHP MySQL Order By
- PHP Limit Data MySql
- PHP JSON
- PHP XML Parsers
- PHP SimpleXML Parser
- PHP SimpleXML - Get Node/Attribute Values
- PHP XML Expat Parser
- PHP XML DOM Parser
- PHP - AJAX intro
- MySQL CREATE VIEW
Magic Constants
Magic Constants
Magic constants are the predefined constants in PHP which get changed on the basis of their use. They start with double underscore (__) and ends with double underscore.
They are similar to other predefined constants but as they change their values with the context, they are called magic constants.
There are eight magical constants defined in the below table. They are case-insensitive.
Name | Description |
---|---|
__LINE__ | Represents current line number where it is used. |
__FILE__ | Represents full path and file name of the file. If it is used inside an include, name of included file is returned. |
__DIR__ | Represents full directory path of the file. Equivalent to dirname(__file__). It does not have a trailing slash unless it is a root directory. It also resolves symbolic link. |
__FUNCTION__ | Represents the function name where it is used. If it is used outside of any function, then it will return blank. |
__CLASS__ | Represents the class name where it is used. If it is used outside of any function, then it will return blank. |
__TRAIT__ | Represents the trait name where it is used. If it is used outside of any function, then it will return blank. It includes namespace it was declared in. |
__METHOD__ | Represents the name of the class method where it is used. The method name is returned as it was declared. |
__NAMESPACE__ | Represents the name of the current namespace. |
Example
Let's see an example for each of the above magical constants.
File Name: magic.php
- <?php
- echo "<h3>Example for __LINE__</h3>";
- echo "You are at line number " . __LINE__ . "<br><br>";// print Your current line number i.e;3
- echo "<h3>Example for __FILE__</h3>";
- echo __FILE__ . "<br><br>";//print full path of file with .php extension
- echo "<h3>Example for __DIR__</h3>";
- echo __DIR__ . "<br><br>";//print full path of directory where script will be placed
- echo dirname(__FILE__) . "<br><br>"; //its output is equivalent to above one.
- echo "<h3>Example for __FUNCTION__</h3>";
- //Using magic constant inside function.
- function cash(){
- echo 'the function name is '. __FUNCTION__ . "<br><br>";//the function name is cash.
- }
- cash();
- //Using magic constant outside function gives the blank output.
- function test_function(){
- echo 'HYIIII';
- }
- test_function();
- echo __FUNCTION__ . "<br><br>";//gives the blank output.
- echo "<h3>Example for __CLASS__</h3>";
- class abc
- {
- public function __construct() {
- ;
- }
- function abc_method(){
- echo __CLASS__ . "<br><br>";//print name of the class abc.
- }
- }
- $t = new abc;
- $t->abc_method();
- class first{
- function test_first(){
- echo __CLASS__;//will always print parent class which is first here.
- }
- }
- class second extends first
- {
- public function __construct() {
- ;
- }
- }
- $t = new second;
- $t->test_first();
- echo "<h3>Example for __TRAIT__</h3>";
- trait created_trait{
- function abc(){
- echo __TRAIT__;//will print name of the trait created_trait
- }
- }
- class anew{
- use created_trait;
- }
- $a = new anew;
- $a->abc();
- echo "<h3>Example for __METHOD__</h3>";
- class meth{
- public function __construct() {
- echo __METHOD__ . "<br><br>";//print meth::__construct
- }
- public function meth_fun(){
- echo __METHOD__;//print meth::meth_fun
- }
- }
- $a = new meth;
- $a->meth_fun();
- echo "<h3>Example for __NAMESPACE__</h3>";
- class name{
- public function __construct() {
- echo 'This line will be printed on calling namespace';
- }
- }
- $clas_name= __NAMESPACE__ .'\name';
- $a = new $clas_name;
- ?>
Output: