php magic constants

__FILE__, __DIR__, __FUNCTION__, __CLASS__, __METHOD__, __TRAIT__ and __NAMESPACE__

 

PHP Magic Contants : Here are eight magical constants in PHP that change depending on where they are used.Some of these magical constants may provide useful information both within object-oriented programs as well as within procedural programs.
The __FILE__ constant is the current filename, __LINE__ is the current line number, __FUNCTION__ is the current function name, __METHOD__ is the current method name (if any), and __CLASS__ is the current class name (if any). Starting with PHP 5.3.0, __DIR__ is the directory that__FILE__ is in and __NAMESPACE__ is the current name‐ space. Starting in PHP 5.4.0, __TRAIT__ is the current trait name (if any).
Their current value depends on the context in which they occur and they all case-insensitive.

Some of the php magic constants and their definitions

Magic constant __LINE__

Magic constantDefinition
__LINE__The current line number of the file. It depends on where you use __LINE__

PHP magic contant Example ⇓__LINE__

the following Example will output the current line number.

Output:

Depending on where you use to output  the constant.That current line number  will be returned.
For me it displays as follows:-
 The current line number is 2

Magic constants

Magic constantDefinition
__LINE__The current line number of the file you are working on.
__FILE__The full path and filename of the file.
__DIR__The directory of the file.The __DIR__ constant tells the program to look in the same directory as the executing code for the files being included.
__FUNCTION__returns the name of the fucntion
__CLASS__returns the name of the class
__METHOD__It returns the method name of a class, this is useful in Object-Oriented PHP. The retuned name is case-sensitive.
__TRAIT__This feature is added in PHP 5.4.0 and it returns the trait as it was declared.
__NAMESPACE__This feature is added in PHP 5.3. and it returns the name of the current namespace as it was declared.

Example ⇓ Magic Constants

We will demonstrate how to use Magic Constants in OOP PHP

Output of the above file is as follows:-
    File name is :/var/www/test/index.php
Method:User::__construct
Method:User::__toString
User Name is : Tetla and Password is : 34_@POL