PHP OOP Magic Method

Source: Internet
Author: User
Tags php oop

1, __construct (): constructor, new object, automatically called

[Public] function __construct ($name ="") {     $this, name = $name;} 

2, __destruct (): destructor, automatically called when an object is destroyed

function __destruct () {     "";}

3. __get (): called automatically when accessing private properties in a class. Pass the Read property name, return the $this-> property name

 function __get ($key) {
            switch ($key) {
                case ' name ':
                    return $this $key ." (This is the text added when __get reads) ";
                case ' age ':
                     return "0". $this $key;
                default:
                     return $this $key;
            }
            return $this $key;
        }

4, __set (): Automatically called when assigning a value to a class's private property. Passing property names and property values that need to be set

function __set ($key, $value) {     if($key = ="name") {  $    $key = $value. " (This is the text on the __set setting!) )";                return ;            }                        $$key = $value;}

5, __isset (): Automatically called when the object private property is detected using Isset (). Pass the detected property name, return Isset ($this property name)

         function __isset ($name) {            if($name = ="sex"return ;             return isset ($this,$name);        }    

6. __unset (): Automatically called when the object private property is deleted using unset (). Pass the deleted function name, execute unset ($this-property name) in the method

        function __unset ($name) {            if($name = ="name")                { " <span style= ' color:red; ' > haha name you can't erase! </span><br>";                 return ;            }             Unset ($$name);        }

7. __tostring (): Automatically called when using Echo to print an object. Returns the content that you want to display when you print the object, which must be a string

        function __tostring () {            = <<<str            The object properties you want to print are as follows:<br/> =             > {$this->name};<br/>            = = {$ this->age};<br/>str            ; return $str;        }        

8, __call (): called automatically when a method that is undefined or not exposed in a class is invoked. Pass the called function name, and the parameter list array

function         __call ($funcName, $funcParams) {            " functions you call: {$funcName}, parameter list " ;            print_r ($funcParams)        }    

9, __clone (): When cloning an object using the Clone keyword, it is called automatically. The function is to initialize the newly cloned object to be assigned the value

        function __clone () {            $this" John Doe ";        }

10, __sleep (): Automatically called when the object is deserialized. Returns an array in which the values in the array are the properties that can be serialized

        function __sleep () {            return Array ("name","age ");  // only name/age two properties can be serialized        }

11, __wakeup (): Automatically called when the object is deserialized. Initializes the newly-generated object for initialization assignment

        function __wakeup () {            $this" John Doe ";        }

12, __autoload (): You need to declare a function outside the class. Called automatically when an undeclared class is instantiated. Pass the instantiated class name and use the class name to load the corresponding class file automatically

    function __autoload ($className) {        include Strtolower ($className). " . class.php "     }

PHP OOP 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.