PHP Object-Oriented-----Magic method

Source: Internet
Author: User

1, __sleep and __wakeup

The serialize () function checks to see if there is a magic method __sleep () in the class. If present, the method is called first 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 any content, it is NULL serialized and produces a E_NOTICE level of error.

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.

user.php (turn from) Big Eyes blog

<?PHPclassUser { Public $name;  Public $id; function__construct () {//assigning an Uniq ID to an ID member        $this->id =uniqid(); }             function__sleep () {//ID members are not serialized here        return(Array(' Name ')); }             function__wakeup () {$this->id =uniqid(); }    } $u=Newuser ();$u->name = "Leo"; $s=Serialize($u);//serialize Serialization Object U, where id attribute is not serialized, ID value discarded $u 2=unserialize($s);//unserialize crossdress, id value is re-assigned//Object U and U2 have different ID assignment Print_r($s);Echo' <br/> '; Print_r($u 2); 

It is clear that the id attribute is no longer included in the $s, and the __wakeup magic method is called in $u 2 to get the id attribute. (at present, the two magic methods do not know deeply, I hope in the next learning to deepen understanding).

2. __tostring method

The __tostring () method is used to respond to a class when it is treated as a string. For example , Echo $obj, what should be shown. This method must return a string, or a E_RECOVERABLE_ERROR level of fatal error will be emitted.

<?PHPclasstestclass{ Public $foo;  Public function__construct ($foo)     {        $this->foo =$foo; }     Public function__tostring () {return $this-foo; }}$class=NewTestClass (' Hello ');Echo $class;?>

If you do not add a magic method, a fatal error is reported.

3. __invoke () method

The __invoke () method is called automatically when an attempt is made to invoke an object in a way that invokes a function.

<? PHP class Callableclass {    function __invoke ($x) {        var_dump($x );    }} $obj New Callableclass; $obj (5); Var_dump (is_callable($obj));? >

4, __get (), __set (), __isset () and __unset ()

PHP provides a " reload" (overloading) that refers to the dynamic " create" class properties and methods. We do this by means of magic methods.

Overloaded methods are called when a class property or method is called that is undefined or not visible under the current environment. these undefined or invisible class properties or methods are addressed by using the inaccessible properties and the inaccessible methods (inaccessible methods) later in this section.

5. Method Overloading

Public mixed __call ( string $name , array $arguments )

PHP Object-Oriented-----Magic method

Related Article

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.