Summary of magic variables in PHP

Source: Internet
Author: User
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 = '';

Public function setName ($ name = '')
{

$ This-> userName = $ name;
}

Function setAddress ($ address = '')
{
$ This-> userAddress = $ address;
}

Function setPhone ($ phone = '')
{
$ This-> userPhone = $ phone;
}

Function _ sleep ()
{
Return array ('useraddress', 'userphone ');
// This will serialize only address and Phone number but not Name
}

}
?>
$ User = new User ();
$ User-> setName ('avinash ');
$ User-> setAddress ('address Here ');
$ User-> setPhone ('20140901 ');

$ SerialData = serialize ($ User );
Echo $ serialData;
?>

OutPut: O: 4: "User": 2: {s: 11: "userAddress"; s: 12: "Address Here"; s: 9: "userPhone"; s: 10: "1234567890 ";}

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 ");

Echo serialize ($ test); // output O: 4: "Test": 1: {s: 8: "mySecret"; s: 28: "You don't want to know my secrets! ";}


_ Wakeup _
Used in Deserialization:
Class User
{
Public $ userName = '';
Public $ userAddress = '';
Public $ userPhone = '';

Public function setName ($ name = '')
{

$ This-> userName = $ name;
}

Function setAddress ($ address = '')
{
$ This-> userAddress = $ address;
}

Function setPhone ($ phone = '')
{
$ This-> userPhone = $ phone;
}

Function _ sleep ()
{
Return array ('useraddress', 'userphone ');
// This will serialize only address and Phone number but not Name
}

Function _ wakeup ()
{
Echo "In Wakeup function "."
";
}

}
?>
$ User = new User ();
$ User-> setName ('avinash ');
$ User-> setAddress ('address Here ');
$ User-> setPhone ('20140901 ');

$ SerialData = serialize ($ User );
Echo $ serialData;
Echo"
";
Var_dump (unserialize ($ serialData ));
?>

Output:
In Wakeup function
Object (User) #2 (3 ){
["UserName"] =>
String (0 )""
["UserAddress"] =>
String (12) "Address Here"
["UserPhone"] =>
String (10) 1234567890"
}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.