PHP Magic Variables and Magic functions in a detailed
This article mainly introduces the PHP magic variables and Magic functions, as well as the use of examples, so that we learn to understand the PHP magic variables and Magic functions, the need for small partners to refer to it.
Magic variable
PHP provides a large number of predefined constants to any script it runs.
However, many constants are defined by different extensions, and are only present when the extensions are loaded, either dynamically after loading, or they are included at compile time.
There are eight magic constants whose values change as they change position in the code.
For example, the value of __line__ depends on the row it is in the script to determine. These special constants are case-insensitive, as follows:
__line__
The current line number in the file.
Instance:
The code is as follows:
Echo ' This is the first '. __line__. ' The line ';
?>
The result of the above example output is:
The code is as follows:
This is the "2" line.
__file__
The full path and file name of the file. If used in the included file, returns the file name that is included.
From PHP 4.0.2, __file__ always contains an absolute path (if it is a symbolic connection, the resolved absolute path), and the previous version sometimes contains a relative path.
Instance:
The code is as follows:
Echo ' The file is located '. __file__. ' ” ';
?>
The result of the above example output is:
The code is as follows:
The file is located in the "E:\wamp\www\test\index.php"
__dir__
The directory where the file resides. If used in the included file, returns the directory where the included files are located.
It is equivalent to DirName (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0)
Instance:
The code is as follows:
Echo ' The file is located '. __dir__. ' ” ';
?>
The result of the above example output is:
The code is as follows:
The file is located in the "E:\wamp\www\test"
__function__
The name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always in lowercase letters.
Instance:
The code is as follows:
function Test () {
Echo ' function name is: '. __function__;
}
Test ();
?>
The result of the above example output is:
The code is as follows:
The function name is: Test
__class__
The name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive).
In PHP 4, this value is always in lowercase letters. The class name includes its declared action area (for example, Foo\bar). Note since PHP 5.4, __CLASS__ has also worked for trait. When used in the trait method, __class__ is the name of the class that invokes the trait method.
Instance:
The code is as follows:
Class Test {
function _print () {
Echo ' class name is: '. __class__. "
";
Echo ' function name is: '. __function__;
}
}
$t = new test ();
$t->_print ();
?>
The result of the above example output is:
The class name is: Test
The function name is: _print
__trait__
Trait's name (PHP 5.4.0 new Plus). Since PHP 5.4.0, PHP has implemented a method of code reuse, called traits.
The Trait name includes its declared function area (for example, Foo\bar).
Members inherited from the base class are overwritten by the Myhelloworld method in the inserted Sayworld Trait. Its behavior is consistent with the methods defined in the Myhelloworld class. The precedence is that methods in the current class override the Trait method, and the trait method overrides the method in the base class.
The code is as follows:
Class Base {
Public Function SayHello () {
Echo ' Hello ';
}
}
{
Public Function SayHello () {
Parent::sayhello ();
Echo ' world! ';
}
}
Class Myhelloworld extends Base {
}
$o = new Myhelloworld ();
$o->sayhello ();
?>
The above routines will output:
The code is as follows:
Hello world!
__method__
The method name of the class (PHP 5.0.0 new addition). Returns the name of the method when it is defined (case-sensitive).
Instance:
Copy the code code as follows:
function Test () {
Echo ' function name is: '. __method__;
}
Test ();
?>
The result of the above example output is:
The code is as follows:
The function name is: Test
__namespace__
The name of the current namespace (case-sensitive). This constant is defined at compile time (PHP 5.3.0 is new).
Instance:
The code is as follows:
namespace MyProject;
Echo ' namespaces are: ' ', __namespace__, ' '; Output "MyProject"
?>
The result of the above example output is:
The code is as follows:
The namespace is: "MyProject"
Magic function
__construct ()
Called when an object is instantiated,
When __construct and a function with a class name are present, __construct is called and the other is not called.
__destruct ()
Called when an object is deleted or when an object operation terminates.
__call ()
Object calls a method,
If the method exists, it is called directly;
If it does not exist, it will call the __call function.
__get ()
When reading the properties of an object,
If the attribute exists, the property value is returned directly;
If it does not exist, the __get function is called.
__set ()
When you set the properties of an object,
If the attribute exists, the value is directly assigned;
If it does not exist, the __set function is called.
__tostring ()
Called when an object is printed. such as Echo $obj; or print $obj;
__clone ()
Called when the object is cloned. such as: $t =new Test (); $t 1=clone $t;
__sleep ()
Serialize before being called. If the object is relatively large, want to cut a bit of the east and then serialize, you can consider this function.
__wakeup ()
Unserialize is called to do some initialization of the object.
__isset ()
Called when detecting whether an object's properties exist. such as: Isset ($c->name).
__unset ()
Called when a property of an object is unset. such as: unset ($c->name).
__set_state ()
Called when the Var_export is called. Use the return value of __set_state as the return value of Var_export.
__autoload ()
When an object is instantiated, the method is called if the corresponding class does not exist.
The above is the whole content of this article, whether the small partners to magic variables and magic function has a new understanding of it, I hope you like the content of this article.
http://www.bkjia.com/PHPjc/960580.html www.bkjia.com true http://www.bkjia.com/PHPjc/960580.html techarticle PHP Magic Variable and Magic function details This article mainly introduces the PHP magic variables and Magic functions, as well as the use of examples to facilitate our learning to understand PHP magic variables and Magic functions, ...