Introduction
In this article you will be learn the PHP "magic" methods. The PHP magic methods start with double underscore characters, as in "_" and another "_" (in other words "_ _"), and they are:
- __construct()
- __destruct()
- __call()
- __sleep()
- __callstack()
- __get()
- __set()
- __unset()
- __walkup()
- __tostring()
- __invoke()
- __setstate()
- __clone()
Why they are called "magic" methods
Its definition is provided by programmers, in other words by you. PHP does not provide the definition of these methods. The programmer writes the code that defines what actually these methods will do. The user cannot call these methods, that is why it is called "magic" methods because PHP will call the functions behind the scene and these methods are always defined within the class. I am providng an example to clarify my point better.
_sleep and _wakeup method in PHP
_sleep()
It is useful when we have a large object in our program and we do not want to save the object completely. It is also used to commit and perform simple clean up tasks.
_wakeup()
It is useful when we want to reconstruct any resource that the object may have, or say it is used to reestablish the database connections that may have been lost.
Example of __sleep and __wakeup magic methods
<?php
class userConnection
{
protected $con;
private $serverName, $userName,$password,$dbname;
public function __construct($serverName, $userName,$password,$dbName)
{
$this->server=$serverName;
$this->username=$userName;
$this->password=$password;
$this->dbanme=$dbName;
$this->connect();
}
private function connect()
{
$this->con=mysql_connect($this->server,$this->username,$this->password);
mysql_select_db($this->dbanme,$this->con);
if (!$this->con)
{
die('Could not connect: ' . mysql_error());
}
echo "Connection is Successfully established.";
}
public function __sleep()
{
return array($serverName, $userName,$password,$dbName);
}
public function _wakeup()
{
this->connect();
}
$obj=new userConnection('localhost','root','','mysql');
?>
Output

Sharad GuptaPosted Feb 6, 2013, 5:30 AM
In PHP all "reserved" function name which is started with __(double underscores) are called as magical methods. If you try to make any user defined function as magical method like suppose A user defined fuction : function myFunction() { //body } It is a simple user defined function, Now if you try to make it magical method like __myFunction(){}, then it will not work as magical method.
Sam HobbsPosted Feb 5, 2013, 11:39 AM
In C/C++ names beginning with an underscore and a capital letter are reserved for the implementation (the compiler). C/C++ programmers often use names beginning with an underscore and a capital letter but that would be a problem if the implementation (the compiler) is enhanced and the enhancement uses the same name that the programmer uses. I don't know if PHP has a corresponding standard related to use of underscores but PHP programmers should determine that. Perhaps the article could be revised or at least any PHP standard or convention related to names beginning with an underscore couldbe posted here as a comment.