PHP magic variables and magic functions. PHP magic variables and magic functions this article briefly introduces PHP magic variables and magic functions, as well as examples for us to learn and understand php magic variables and magic functions, PHP magic variables and magic functions
This article briefly introduces PHP magic variables and magic functions, as well as examples for us to learn and understand php magic variables and magic functions. For more information, see.
Magic variable
PHP provides a large number of predefined constants to any script it runs.
However, many constants are defined by different extension libraries. they only appear when these extension libraries are loaded, dynamically loaded, or included during compilation.
There are eight magic constants whose values change with their positions in the code.
For example, the value of _ LINE _ depends on the rows in the script. These special constants are case-insensitive, as follows:
_ LINE __
The current row number in the file.
Instance:
The code is as follows:
Echo 'This is LINE'. _ LINE ';
?>
The output result of the above instance is:
The code is as follows:
This is line 2
_ FILE __
The complete file path and file name. If it is used in a file to be included, the file name to be included is returned.
Starting from PHP 4.0.2, __file _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution ), earlier versions sometimes contain a relative path.
Instance:
The code is as follows:
Echo 'The FILE is in '. _ FILE __.'"';
?>
The output result of the above instance is:
The code is as follows:
This file is located in "E: \ wamp \ www \ test \ index. php"
_ DIR __
The directory where the file is located. If it is used in included files, the Directory of the included files is returned.
It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0)
Instance:
The code is as follows:
Echo 'The file is located in '. _ DIR __.'"';
?>
The output result of the above instance is:
The code is as follows:
This file is located in "E: \ wamp \ www \ test"
_ FUNCTION __
Function name (new PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters.
Instance:
The code is as follows:
Function test (){
Echo 'function name: '. _ FUNCTION __;
}
Test ();
?>
The output result of the above instance is:
The code is as follows:
Function name: test
_ CLASS __
Class name (new PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ).
In PHP 4, the value is always lowercase letters. The class name includes the declared zone (for example, Foo \ Bar ). Note that _ CLASS _ from PHP 5.4 also applies to trait. When used in the trait method, __class _ is the name of the CLASS that calls the trait method.
Instance:
The code is as follows:
Class test {
Function _ print (){
Echo 'CLASS name:'. _ CLASS __."
";
Echo 'function name: '. _ FUNCTION __;
}
}
$ T = new test ();
$ T-> _ print ();
?>
The output result of the above instance is:
Class name: test
Function name: _ print
_ TRAIT __
Trait name (new in PHP 5.4.0 ). Since PHP 5.4.0, PHP has implemented a method for code reuse, called traits.
The Trait name includes the declared region (for example, Foo \ Bar ).
The member inherited from the base class is overwritten by the MyHelloWorld method in the inserted SayWorld Trait. Its behavior is the same as that defined in the MyHelloWorld class. The priority is that the methods in the current class will overwrite the trait method, and the trait method overwrites the methods 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 routine will output:
The code is as follows:
Hello World!
_ METHOD __
Class method name (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).
Instance:
The code is as follows:
Function test (){
Echo 'function name: '. _ METHOD __;
}
Test ();
?>
The output result of the above instance is:
The code is as follows:
Function name: test
_ NAMESPACE __
Name of the current namespace (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0 ).
Instance:
The code is as follows:
Namespace MyProject;
Echo 'namespace: "', _ NAMESPACE __,'" '; // output "MyProject"
?>
The output result of the above instance is:
The code is as follows:
Namespace: "MyProject"
Magic functions
_ Construct ()
Called When instantiating an object,
When _ construct and a function with the class name both exist, __construct will be called and the other will not be called.
_ Destruct ()
It is called when an object or object operation is terminated.
_ Call ()
Object calls a method,
If a method exists, it is called directly;
If not, the _ call function is called.
_ Get ()
When reading the attributes of an object,
If a property exists, the property value is directly returned;
If not, the _ get function is called.
_ Set ()
When setting attributes of an object,
If an attribute exists, the value is assigned directly;
If not, the _ set function is called.
_ ToString ()
It is called to print an object. Such as echo $ obj; or print $ obj;
_ Clone ()
Called when cloning an object. For example: $ t = new Test (); $ t1 = clone $ t;
_ Sleep ()
Serialize is called before. If the object is large and you want to delete something and serialize it, consider this function.
_ Wakeup ()
Unserialize is called to initialize objects.
_ Isset ()
It is called to check whether an object property exists. For example, isset ($ c-> name ).
_ Unset ()
Unset an object property is called. For example, unset ($ c-> name ).
_ Set_state ()
Called when var_export is called. Use the return value of _ set_state as the return value of var_export.
_ Autoload ()
When instantiating an object, if the corresponding class does not exist, this method is called.
The above is all about this article. do you have a new understanding of magic variables and magic functions? I hope you will like this article.
Http://www.bkjia.com/PHPjc/960580.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/960580.htmlTechArticlePHP magic variables and magic functions detailed description this article mainly introduces PHP magic variables and magic functions, as well as examples of use, so that we can learn to understand php magic variables and magic functions ,...