The Magic Method of PHP

Source: Internet
Author: User
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}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.