__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state () and __clone() are called "Magic methods" in PHP. You cannot use these method names when naming your own class methods unless you want to use their magic functionality.
PHP reserves all class methods starting with __ (two underscores) as magic methods. Therefore, when defining class methods, except for the above magic methods, it is recommended not to prefix them with __.
__sleep() and __wakeup()
publicarray__sleep (void)
void__wakeup (void)
serialize() function will check whether there is a magic method __sleep() in the class. If present, this method will be called first, and then the serialization operation will be performed. This function can be used to clean an object and return an array containing the names of all variables in the object that should be serialized. If the method returns nothing, NULL is serialized and an E_NOTICE level error is generated.
Note:
__sleep() cannot return the name of the private member of the parent class. Doing so will generate an E_NOTICE level error. You can use the Serializable interface instead. The
__sleep() method is often used to submit uncommitted data, or similar cleaning operations. At the same time, this function is useful if you have some large objects but do not need to save them all.
In contrast, unserialize() checks whether there is a __wakeup() method. If it exists, the __wakeup method will be called first to prepare the resources needed by the object in advance.
__wakeup() is often used in deserialization operations, such as re-establishing a database connection, or performing other initialization operations.
Example #1 Sleep and wakeup
class Connection { protected $link; private $server,$username,$password,$db; public function __construct($server,$username,$password,$db) { $this->server = $server; $this->username = $username; $this->password = $password; $this -> db = $db; $this -> connect(); } private function connect(){ $this -> link = mysql_connect($this->server,$this->username,$this->password); mysql_select_db($this->db,$this->link); } public function __sleep(){ return array('server','username','password','db'); } public function __wakeup(){ $this->connect(); } }
__toString()
public string __toString ( void )
__toString() method is used for how a class should respond when it is treated as a string. For example, echo $obj; should display something. This method must return a string, otherwise a fatal error of level E_RECOVERABLE_ERROR will be issued.
You cannot throw exceptions in the __toString() method, doing so will result in a fatal error.
Example #2 Simple example
class TestClass { public $foo; public function __construct($foo) { $this->foo = $foo; } public function __toString(){ return $this->foo; } } $class = new TestClass('Hello'); echo $class;
Output result:
Hello
It should be pointed out that before PHP 5.2.0, the __toString() method can only take effect when used directly in echo or print. After PHP 5.2.0, it can be used in any string environment (such as through printf(), using the %s modifier), but cannot be used in non-string environments (such as using the %d modifier). As of PHP 5.2.0, converting an object that does not define the __toString() method to a string will generate an E_RECOVERABLE_ERROR level error.
__invoke()
mixed__invoke ([ $... ] )
When trying to call an object by calling a function, the __invoke() method will be automatically called.
Note:
This feature is only available in PHP 5.3.0 and above.
Example #3 Use __invoke()
class CallableClass { function __invoke($x){ var_dump($x); } } $obj = new CallableClass; $obj(5); var_dump(is_callable($obj));
Output result:
int(5)
bool(true)
__set_state()
static object __set_state ( array $properties )
Since PHP 5.1.0 This static method is called when var_export() is called to export a class.
The only parameter of this method is an array, which contains class properties arranged in the format of array('property' => value, ...) .
Example #4 Using __set_state()> (from PHP 5.1.0)
class A { public $var1; public $var2; public static function __set_state($an_array) { $obj = new A; $obj -> var1 = $an_array['var1']; $obj -> var2 = $an_array['var2']; return $obj; } } $a = new A; $a->var1 = 5; $a->var2 = 'foo'; eval('$b='.var_export($a,true).';'); var_dump($b);
Output result:
object(A)#2 (2) { ["var1"]=> int(5) [" var2"]=> string(3) "foo" }