Home >Backend Development >PHP Problem >What are the php magic methods?
php magic method: 1. [_sleep()] controls the actual processing part when the object is serialized; 2. [_wakeup()] restores the object attributes after deserialization; 3. [_toString()] Mechanism for converting objects into strings.
The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer.
php magic method:
_sleep() can control the part that is actually processed when the object is serialized
_wakeup() restores after deserialization Object attributes
_toString() Mechanism for converting objects into strings
Convert PHP variables into a string of encoded strings, the method is serialize() Deserialize unserialize()
//序列化 class testSerialize{ public $a = 10; public $b = 15; public $c = 20; function _construct(){ $this->b = $this->a * 10; $this->c = $this->b * 2; } } $k = serialize(new testSerialize()); echo $k;// out: O:13:"testSerialize":3:{s:1:"a";i:10;s:1:"b";i:15;s:1:"c";i:20;} $j = unserialize($k);
sleep method:
class testSerialize1{ public $a = 10; public $b = 15; public $c = 20; function _construct(){ $this->b = $this->a * 10; $this->c = $this->b * 2; } function __sleep(){ return $this->a; } } $k = serialize(new testSerialize1()); echo $k;
The same applies to other methods
Related video recommendations: PHP programming from entry to proficiency
The above is the detailed content of What are the php magic methods?. For more information, please follow other related articles on the PHP Chinese website!