• In PHP magic constants are the predefined constants which is used on the basis of their use.
  • Constants are created by various extensions and there are nine magic constants in the PHP and all of the constant resolved at the compile-time, not like the regular constant which is resolved at run time.
  • In this there are eight magic constants which start and end with double underscores (__), they are.
    • __line__
    • __file__
    • __dir__
    • __function__
    • __class__
    • __method__
    • __namespace__
    • __trait__

__line__

  • This type of magic constant returns the current line number of the file.
  • In our program file if we use this magic constant somewhere then this constant will display the line number during compile time.

Syntax

         __line__

__file__

  • This type of magic constant return the full path of the executed file with the name of the file.

Syntax

         __file__

__dir__

  • This type of magic constant returns the directory of the executed file.

Syntax

          __dir__

__function__

  • This type of magic constant returns the name of the function where it is included.

Syntax

         __function__

__class__

  • This type of magic constant returns the name of the class where it is included.

Syntax

__class__

__method__

  • This type of magic constant returns the method name where it is included.

Syntax

       __method__

__namespace__

This type of magic constant returns the current namespace where it is included.

Syntax

       __namespace__

__trait__

  • This type of magic constant returns the trait name where it is included.

Syntax

  __trait__

Categorized in: