PHP Magic Method

Source: Internet
Author: User

First, the Magic method in PHP

PHP has some special methods for object-oriented programming, which are called automatically at certain times, and we call this method magic.

Second, the common magic methods and functions

1. Constructor __construct (): Called automatically when a class is instantiated as an object, primarily used to initialize properties when an object is built.

Cases:

1<?PHP2 3 classapp{4 5       Public function__construct () {6            7                 Echo1111;8     }9     Ten } One  A $app=NewAPP ();//Auto Output 111

2. Destructor __destruct (): When the object is finished, it is automatically called before the object is destroyed.

Cases:

1<?PHP2 3 classapp{4 5       Public function__construct () {6            7                 Echo1111;8     }9      Ten       Public function__destruct () { One      A          Echo2222; - } - $app=NewAPP ();//first output 111, in the output 222

3.__isset ($name): The __isset ($name) method is automatically triggered if the property is private or undefined if an attribute exists in the outer detection class.

Cases

<?PHPclassapp{Private $name= ' Jack ';  Public function__isset ($name){             //$name The property name for the external query          if(isset($this-$name)){                   Echo' The request is not legal, you are querying a private attribute '; }Else{                  Echo' Undefined attribute '; }    }    }$app=NewApp ();isset($app-name);//Result: The request is not legal, you are querying the private attribute;isset($app-Age );//Result: undefined attribute

4.__unset () automatically triggers the __unset method when the property in the class is removed externally, if the property is a private property of the class, or if the property is undefined.

Cases:

<?PHPclassapp{Private $name= ' Jack ';  Public function__unset ($name){             //$name The name of the property to be removed externally          if(isset($this-$name)){                   Echo' The request is illegal, you delete the private attribute '; }Else{                  Echo' Undefined attribute '; }    }    }$app=NewApp ();unset($app-name);//Result: The request is illegal, you delete the private attribute;unset($app-Age );//Result: undefined attribute

5. __set ($name, $value): __set ($name, $value) is triggered if the property is not defined when it is assigned to an attribute in the class externally.

Cases

<? PHP class app{      publicfunction __set ($name) {             //$ Name is external to assign the property name       echo 2222;}    } $app=new  app (); $app->name= ' Jack '; // Results: 2222

6.__get ($name): When the property of a class is called externally, the __get ($name) method is triggered if the property is not defined.

Cases

<?PHPclassapp{ Public $name= ' Jack ';  Public function__get ($name){             //$name The name of the property to be called externally       Echo333; }    }$app=NewApp ();Echo $app->name;//output jack;Echo $app->age;//results: 333;

7.__call ($name): When a method in a class is called externally, the __call ($name) method is triggered if the method is undefined.

Cases

<?PHPclassapp{ Public functionrun () {Echo' To travel '; }  Public function__call ($name){             //$name The name of the method to be called externally       Echo333; }    }$app=NewApp ();Echo $app->run ();//To travel in the distance;Echo $app->play ();//results: 333;

8. __autoload ($name): Generally used to automatically load classes, if the naming conventions of the class, and to refer to more than the analogy, or to prevent forgetting the reference, you can use the automatic loading function.

Cases

<? PHP function __autoload ($name) {   include   $name. ' Class.php ';} $app=new app (); // will automatically load the App.class.php in the current directory

PHP Magic Method

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.