Examples of overloads in PHP and several common magic methods

Source: Internet
Author: User

In object-oriented languages, there is a very important concept--overload, that is, overloading. Overloading is typically a method for implementing several overloads within a class that have the same names and different parameter forms. In PHP, however, this concept has nothing to do with "overloading" in most object-oriented languages, which is irrelevant at all.

Overloading in PHP refers to the dynamic creation of class properties and methods. Overloaded methods are called when a class property or method is called that is undefined or not visible under the current environment. The straightforward point, then, is a processing mechanism that accesses a class or object's inaccessible properties or methods . (not accessible, such as non-existent or restricted by access to the keyword)

1. Property overloading

Four Magic methods:

1) __get ($ attribute name)

This method is called automatically when accessing an object that is not accessible

2) __set ($ property name, $ property value)

This method is called automatically when a value is assigned to an inaccessible property in an object. The two parameters in the method, which in turn represent the properties that are accessed and the corresponding values.

3) __isset ($ attribute name)

This method is called automatically when a property that is inaccessible to an object is isset () or empty ().

4) __unset ($ attribute name)

This method is called automatically when a property that is inaccessible to an object is unset () destroyed.

The code used is as follows:

<?PHPHeader("Content-type:text/html;charset = Utf-8"); classtest{ Public $p 1= 1; function__get ($property) {//This method is automatically called when access to an inaccessible property      Echo"Inaccessible Properties: {$property}"; }    function__set ($property,$value) {//Assigning a value to an inaccessible property automatically calls the method      Echo"For inaccessible Properties {$property} Assignment {$value}<br/> "; }    function__isset ($property){//The method is called automatically when Isset is judged on an inaccessible property .      Echo"For inaccessible Properties {$property} Make Isset judgment <br/> "; }    function__unset ($property){//The method is automatically called when the unset operation is performed on a property that is not accessible      Echo"For inaccessible Properties {$property} for unset operation <br/> "; }  }  $t=NewTest (); Echo $t->p1. " <br/> "; Echo $t->p2. " <br/> "; //assigning values to inaccessible properties  $t-&GT;P3 = 2; //isset judgment on inaccessible properties  isset($t-p4); //unset operations on inaccessible properties  unset($t-p5); /*Run Result: 1 inaccessible properties: P2 P3 assignment to inaccessible properties P4 isset judgment on inaccessible properties P5 for UN-accessible properties Set Operation*/?>

Mates are using the __get () and __set () methods to make classes more flexible and free, which makes it easy to add properties to a class. As follows:

<?PHPHeader("Content-type:text/html;charset = Utf-8"); classtest{//defines an array for storing inaccessible properties    protected $plist=Array(); function__get ($property) {//the method is called automatically when accessing the Inaccessible property//property is inaccessible, read from plist      if(isset($this->plist[$property])) {        return $this->plist[$property]; }      Else {        returnProperties$property} not accessible "; }    }    function__set ($property,$value) {//Assigning a value to an inaccessible property automatically calls the method//the property is not accessible and is stored in the plist      $this->plist[$property] =$value; }    function__isset ($property){//The method is called automatically when Isset is judged on an inaccessible property .      $v=isset($this->plist[$property]); return $v; }    function__unset ($property){//The method is automatically called when the unset operation is performed on a property that is not accessible      unset($this->plist[$property]); }  }  $t=NewTest (); $t-&GT;P1 = 1;//Assigning a value to an inaccessible property  Echo"The value of the Inaccessible property P1 is:".$t->p1. " <br/> ";//Use the P1 property (after the previous line of code, P1 has been added to the plist)  Echo"The value of the Inaccessible property P2 is:".$t->p2. " <br/> ";//no assignment to P2//isset operation for P1 and P2 respectively  $r 1=isset($t-&GT;P1);//The result is true, but returns False if the __isset magic method is not handled  $r 2=isset($t-&GT;P2);//The result is false, and it does not exist ...  Var_dump($r 1);Echo"<br/>"; Var_dump($r 2);Echo"<br/>"; //unset operation for P1 and P2 respectively  unset($t-p1); unset($t-p2); Echo"The value of the non-existent property P1 is:".$t->p1. " <br/> "; Echo"The value of the non-existent property P2 is:".$t->p2. " <br/> "; /*Result: The value of the Unreachable property P1 is: 1 The value of the unreachable property P2 is: The property P2 is not accessible bool (TRUE) bool (false) The value of the non-accessible property P1 is: Property P1 Inaccessible Property P2 value is: property P2 Inaccessible*/?>

2. Method overloading

1) __call ()

The method is called automatically when the object's inaccessible method is called.

2) __callstatic ()

This method is called automatically when a class's inaccessible static method is called.

Examples of overloads in PHP and several common magic methods

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.