PHP retains all class methods that begin with __ (two underscores) as a magic method. So in the definition method, in addition to the Magic method, it is recommended not to use two underscore prefixes.
Magic Method (Magic methods) has __construct (), __destruct (), __call (), __callstatic (), __get (), __set (), __isset (), __unset (), __ Sleep (), __wakeup (), __tostring (), __invoke (), __set_state (), __clone (), and __debuginfo () methods.
So let's talk about the difference between __sleep () and __wakeup ():
Grammar:
1 public array __sleep (void) 2 3 void __wakeup (void)
The serialize () function checks to see if there is a magic method __sleep () in the class. If present, the method is called back before the serialization operation is performed. This feature can be used to clean up an object and return an array containing all the variable names that should be serialized in the object. If the method does not return anything, NULL is serialized and a E_notice level error is generated.
The __sleep () method is commonly used to commit uncommitted data and to live a similar cleanup operation. At the same time, if there are some very large objects, but do not need to save all, this function is very useful.
In contrast, unserialize () checks for the existence of a __wakeup () method. If present, the __wakeup method is called before the resource required by the object is prepared beforehand.
__wakeup () is often used in deserialization operations, such as re-establishing a database connection, or performing other initialization operations.
1
server= $server, 8 $this->username= $username, 9 $this->password= $password, ten $this->db= $db; 11 $ This->connect ()}13 private Function Connect () {$this->link=mysql_connect ($this->server, $this Username, $this->password); mysql_select_db ($this->db, $this->link);}17 public Function __sleep () {18 Return array (' Server ', ' username ', ' password ', ' db '),}20 public Function __wakeup () {$this->connect (); 22}23}