YouTip LogoYouTip

Php Magic Constant

# PHP Magic Constants PHP provides a large number of predefined constants to any script which it runs. However many of these constants are defined by various extensions, and will only be present when those extensions are available, either dynamically loaded or compiled in. There are eight magic constants that change their values based on where they are used in code. For example the value of `__LINE__` depends on the line that it is used on. These special constants are case-insensitive and are as follows: * * * ## __LINE__ The current line number of the file. ## Example <?php echo 'This is line ' . __LINE__ . '
'; ?> The above example will output: This is line 2 * * * ## __FILE__ The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, `__FILE__` always contains an absolute path (whereas in older versions it contained a relative path). Example: ## Example <?php echo 'The file is ' . __FILE__ . '
'; ?> The above example will output: The file is E:wampwwwtestindex.php * * * ## __DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. It is equivalent to `dirname(__FILE__)`. Unless it is the root directory, the directory name will not include a trailing slash. (Added in PHP 5.3.0) ## Example <?php echo 'The file is located in ' . __DIR__ . '
'; ?> The above example will output: The file is located in E:wampwwwtest * * * ## __FUNCTION__ The function name (Added in PHP 4.3.0). As of PHP 5 this constant returns the name of the function as it was declared (case-sensitive). In PHP 4 its value is always lowercased. ## Example The above example will output: Function name: test * * * ## __CLASS__ The class name (Added in PHP 4.3.0). As of PHP 5 this constant returns the name of the class as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g. `FooBar`). Note that as of PHP 5.4 `__CLASS__` works also in traits. When used in a trait method, `__CLASS__` is the name of the class the trait is used in. ## Example <?php class test { function _print() { echo 'Class name: ' . __CLASS__ . "
"; echo 'Function name: ' . __FUNCTION__; } } $t = new test(); $t->_print(); ?> The above example will output: Class name: test Function name: _print * * * ## __TRAIT__ The trait name (Added in PHP 5.4.0). As of PHP 5.4.0 PHP implements a method of code reuse called traits. Trait names include the namespace it was declared in (e.g. `FooBar`). Members of a trait inserted into a class will override a method of the same name from a base class. The behavior is consistent with the method defined in the class. The precedence order is that methods from the current class will override trait methods, which in turn override methods from a base class. ## Example sayHello(); ?> The above example will output: Hello World! * * * ## __METHOD__ The class method name (Added in PHP 5.0.0). Returns the method name as it was declared (case-sensitive). Example: ## Example The above example will output: Method name: test * * * ## __NAMESPACE__ The current namespace name (case-sensitive). This constant is defined at compile-time (Added in PHP 5.3.0). Example: ## Example The above example will output: Namespace: "MyProject"
← Bootstrap V2 Tab PluginFunc Array Replace Recursive β†’