Common magic Methods for PHP

Source: Internet
Author: User

__construct: constructor, once the object is created, it is automatically called

__call: Automatically triggers when undefined methods are called

__set: Automatically triggers when a value is set to a property outside the class that is not accessible

__get: Automatically fires when a property that is not accessible outside the class is obtained

__isset: Automatically fires when isset () or empty () is used for properties that are not accessible outside the class

__unset: Automatically fires when unset () is used for properties that are not accessible outside the class

__destruct: Automatically triggers when an object ends its life cycle

<?php header ("Content-type:text/html;charset=utf-8"), class magic{private $testGet;       Private Test __get The properties of the Magic method private $testSet;     Private Test __set The properties of the Magic method private $testIsset;     Private Test __isset The properties of the Magic method private $testUnset;     Private Test __unset The properties of the Magic method public static $a = 1;         The private constants used to test the __clone Magic method/** * Test the composition method, call this method first when creating a new object */Public function __construct () {echo static:: $a +2; echo "This is the constructor." <br> "."    <br> "; }/** * Tests the Isset method, which is triggered when the inaccessible property is called by the isset () or empty () method when the */Public Function __isset ($name) {echo "pair is inaccessible Properties: ". $name." Call Isset () or empty () "." <br> "."    <br> "; }/** * Tests the unset method, which is triggered when the inaccessible property is called the unset () method when the */Public Function __unset ($name) {echo "When the property is unreachable:". $na Me. " Call unset () "." <br> "."    <br> "; }/** * Tests the call method, which is triggered when an undefined method is called */Public Function __call ($method, $agrs) {echo "called a nonexistent Method:". $method ." <br> "."        This is the received parameter: ";        Print_r ($AGRS); echo "<bR> "."    <br> "; }/** * Tests the Set method, which is triggered when an inaccessible property is set (*/Public function __set ($name, $data) {echo "called a Non-set property:". $name. " <br> "." The parameters are: ". $data."    <br> "; /** * Tests the Get method, which is triggered when an inaccessible property is accessed (*/Public Function __get ($name) {echo "called the Unreachable Property:". $name. " <br> "."    <br> ";        }/** * Tests the Clone method, which is triggered when the class is cloned */Public Function __clone () {echo static:: $a +1; echo "I've been cloned." <br> "."    <br> ";    }/** * Test the destructor method, which is called when the script is closed */Public Function __destruct () {echo "This is a destructor"; }} $test =new Magic ();   Instantiate the Magic class, triggering the constructor $test->callmethod (' 123 ');     Call a method that does not exist $test->testset=123;         Set properties outside the class that are not accessible $test->testget;     Gets an inaccessible property outside the class $test2=clone $test;       Cloning the test class, opens up another identical property and method storage space, but does not affect Isset ($test->testisset);        Use Isset to perform an inaccessible property unset ($test->testunset) outside the class; Using unset to perform non-accessible properties outside the class

Common magic Methods for PHP

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.