PHP Magic Variable Summary php, there are a lot of good magic variables, with good, can do less, the following summary:
1 _line_
Displays the number of lines in the current code:
echo "This was line number:". __line__;
2 _file_
Displays the path of the current file
3 _method_
Displays the name of the current method, such as
Class Magicconstant
{
function __construct ()
{
echo "This is function a";
}
Function B ()
{
echo "<br/>";
echo "This is function B";
echo "<br/>";
Echo __method__;
}
}
$cm = new Magicconstant ();
$CM->b ();
Show
This is function a
This is function b
Magicconstant::b
4 _function_
Displays the name of the current function
Function B ()
{
echo "<br/>";
echo "This is function B";
echo "<br/>";
Echo __function__;
}
Output:
This is function b
Magicconstant::b
5 _dir_
Displays the current directory name, such as
echo "The directory name is:". __dir__;
Output:
The directory name is:d:\wamp\www
6 _class_
Displays the current class
Class Magicconstant
{
function __construct ()
{
echo "The class name is:". __class__;
}
}
$cm = new Magicconstant ();
Show:
The class name Is:magicconstant
7 _namespace_
Show current namespace
Namespace Magicconstant
{
echo "The namespace is:". __namespace__;
}
Output:
The namespace Is:magicconstant
8 _sleep_
_sleep_ is used before the class is serialized,
<?php
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 would serialize only address and Phone number but not Name
}
}
?>
<?php
$User = new User ();
$User->setname (' Avinash ');
$User->setaddress (' Address here ');
$User->setphone (' 1234567890 ');
$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 if there is a __sleep () in the class, and if so, the function runs before any serialization. The function must return an array of member properties that need to be persisted and serialize only those member properties returned by the function. The function has two functions: first. Before serializing, close any database connections that the object may have, and so on. Second. Specifies the member properties that need to be serialized in the object, and if a property is larger and does not need to be stored, it can not be written into the array to be returned by __sleep () so that the property is not serialized
Another example:
Class Test {
Public $mySecret; My secret doesn't want anyone to know.
Public function __construct ($secret) {
$this->mysecret = $secret;
}
Public Function __sleep () {
$this->mysecret = "You never know my secret!";
Return Array (' MySecret '); Be sure to return a variable, otherwise the return is empty, 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'll never know my secret!";}
_wakeup_
For deserialization:
<?php
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 would serialize only address and Phone number but not Name
}
function __wakeup ()
{
echo "in Wakeup function". " <br/> ";
}
}
?>
<?php
$User = new User ();
$User->setname (' Avinash ');
$User->setaddress (' Address here ');
$User->setphone (' 1234567890 ');
$serialData = serialize ($User);
Echo $serialData;
echo "<br/>";
Var_dump (Unserialize ($serialData));
?>
Output:
In Wakeup function
Object (User) #2 (3) {
["UserName"]=>
String (0) ""
["UserAddress"]=>
String (page) "Address here"
["Userphone"]=>
String (10) "1234567890"
}
Summary of magic Variables in PHP