Nov 22

php magic constant

__LINE__              : Current line number of file.

echo "This is line number " . __LINE__ . ".\n";

__FILE__                  : The Full path and Filename of the file. If used inside an include the name of include file return.
echo "This file full path and file name is '" . __FILE__ . "'.\n"
__DIR__                   : The directory of the file. If used inside an include, the directory of the included file is returned. This equivalent to dirname(__FILE__).

__FUNCTION__     : Return Function Name.

__CLASS__               : Return Class Name. The Class name include the namespace.

__TRAIT__               : Return Trait Name. The Trait name include the namespace.

__METHOD__        : The class method name.

__NAMESPACE__    : The name of the current namespace.


Test Some Magic Constant

class MagicConstants {

public function printClass() {
echo “This is ” . __CLASS__ . ” class.\n”;
public function printMethod() {
echo “This is ” . __METHOD__ . ” method.\n”;

public function printFunction() {
echo “This is function ‘” . __FUNCTION__ . “‘ inside class.\n”;

public function printNamespace() {
echo “Namespace name is ‘” . __NAMESPACE__ . “‘.\n”;

$magic_constants = new MagicConstants;





1 comment

  1. WilliamDacy

    Very informative article.Really looking forward to read more. Great. Tommy

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>