Latest web development tutorials

PHP variable Magic

Any PHP script to run it provides a number of predefined constants.

However, many constants are defined by various extensions, and only loaded when these extensions will appear, or dynamically loaded, or has been included at compile time into it.

There are eight Magic constants and their values ​​as they change position in the code is changed.

For example __LINE__ value on which it is dependent on the line in the script to decide. These special constants are case-insensitive, as follows:


__LINE__

Files in the current line number.

Example:

<?php
echo '这是第 “ '  . __LINE__ . ' ” 行';
?>

The above example output is:

这是第 “ 2 ” 行

__FILE__

The full path and filename of the file. If used in the included file, then return the file name to be included.

Since PHP 4.0.2 onwards, __ FILE__ always contains an absolute path (if it is a symbolic link, it is an absolute path parsed), and in this previous version sometimes contain a relative path.

Example:

<?php
echo '该文件位于 “ '  . __FILE__ . ' ” ';
?>

The above example output is:

该文件位于 “ E:\wamp\www\test\index.php ”

__DIR__

Catalog file is located. If included in the file, the directory is included in the file is returned.

It is equivalent to dirname (__ FILE__). Unless it is the root directory, or directory name does not include a slash at the end. (PHP 5.3.0 add)

Example:

<?php
echo '该文件位于 “ '  . __DIR__ . ' ” ';
?>

The above example output is:

该文件位于 “ E:\wamp\www\test ”

__FUNCTION__

Function name (PHP 4.3.0 added). Since PHP this constant returns the function name as it was declared 5 cases (case-sensitive). In PHP 4 its value is always lowercase.

Example:

<?php
function test() {
	echo  '函数名为:' . __FUNCTION__ ;
}
test();
?>

The above example output is:

函数名为:test

__CLASS__

Name of the class (PHP 4.3.0 added). Since PHP this constant returns the class name is defined as the time from 5 (case-sensitive).

In PHP 4 its value is always lowercase. Class name including the role of the area which is declared (such as Foo \ Bar). Note from PHP 5.4 onwards __CLASS__ of trait also play a role. When used in trait methods, __ CLASS__ trait method is to call the name of the class.

Example:

<?php
<?php
class test {
	function _print() {
		echo '类名为:'  . __CLASS__ . "<br>";
		echo  '函数名为:' . __FUNCTION__ ;
	}
}
$t = new test();
$t->_print();
?>

The above example output is:

类名为:test
函数名为:_print

__TRAIT__

Trait name (PHP 5.4.0 added). Starting from PHP 5.4.0, PHP implements a method of code reuse, called traits.

Trait name including the role of the area which is declared (such as Foo \ Bar).

Is covered from the base class inherits members inserted SayWorld Trait in MyHelloWorld method. MyHelloWorld behavior consistent approach defined in the class. Priority is the current method in a class overrides trait method, method and trait covering the base class method.

<?php
class Base {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait SayWorld {
    public function sayHello() {
        parent::sayHello();
        echo 'World!';
    }
}

class MyHelloWorld extends Base {
    use SayWorld;
}

$o = new MyHelloWorld();
$o->sayHello();
?>

The above example will output:

Hello World!

__METHOD__

Methods for classes (PHP 5.0.0 added). This method returns the name as it was declared (case-sensitive).

Example:

<?php
function test() {
	echo  '函数名为:' . __METHOD__ ;
}
test();
?>

The above example output is:

函数名为:test

__NAMESPACE__

Name of the current namespace (case-sensitive). This constant is defined at compile time (PHP 5.3.0 added).

Example:

<?php
namespace MyProject;

echo '命名空间为:"', __NAMESPACE__, '"'; // 输出 "MyProject"
?>

The above example output is:

命名空间为:"MyProject"