Summary of magic variables in PHP there are a lot of good magic variables in PHP, which can be used to get twice the result with half the effort. Below is a summary: using line _ & nbsp; shows the number of lines of the current code: & nbsp; echo "Thisislinenumber :". _ LINE __; 2 Summary of magic variables in PHP
In PHP, there are a lot of good magic variables that can be used to get twice the result with half the effort. Below is a summary:
1 _ LINE _
Display the number of lines of the current code:
Echo "This is line number:". _ LINE __;
2 _ FILE _
Display the path of the current file
3 _ METHOD _
Display the name of the current method, such
Class Magicconstant
{
Function _ construct ()
{
Echo "This is function ";
}
Function B ()
{
Echo"
";
Echo "This is function B ";
Echo"
";
Echo _ METHOD __;
}
}
$ Cm = new Magicconstant ();
$ Cm-> B ();
Display
This is function
This is function B
Magicconstant: B
4 _ FUNCTION _
Display the name of the current function
Function B ()
{
Echo"
";
Echo "This is function B ";
Echo"
";
Echo _ FUNCTION __;
}
Output:
This is function B
Magicconstant: B
5 _ DIR _
Displays the current directory name, as shown in figure
Echo "The directory name is:". _ DIR __;
Output:
The directory name is: D: \ wamp \ www
6_class _
Display current class
Class Magicconstant
{
Function _ construct ()
{
Echo "The class name is:". _ CLASS __;
}
}
$ Cm = new Magicconstant ();
Display:
The class name is: Magicconstant
7 _ NAMESPACE _
Display the current namespace
Namespace MagicConstant
{
Echo "The namespace is:". _ NAMESPACE __;
}
Output:
The namespace is: MagicConstant
8 _ sleep _
_ Sleep _ is used before class serialization,
Class User
{
Public $ userName = '';
Public $ userAddress = '';
Public $ userPhone = '';
When the program runs, serialize () checks whether the class has _ sleep (). If yes, the function will run before any serialization. this function must return an array of member attributes to be serialized and saved, and only serialize these member attributes returned by this function. this function has two functions: 1. close any database connection that the object may have before serialization. second. specify the member attribute to be serialized in the object. if a property is large and does not need to be stored, you can not write it into the array to be returned by _ sleep, this property will not be serialized.
Another example:
Class Test {
Public $ mySecret; // do not want to know my secrets
Public function _ construct ($ secret ){
$ This-> mySecret = $ secret;
}
Public function _ sleep (){
$ This-> mySecret = "You don't want to know my secrets! ";
Return array ('mysecret'); // The variable must be returned. otherwise, null is returned, so there is nothing to serialize.
}
}
$ Test = new Test ("I love someone in my heart ");
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.