PHP Magic Method overloading

Source: Internet
Author: User

Overload

Property overloading and method overloading

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.

all overloaded methods must be declared as Public .

Property overloading

Public void __set ( string $name , mixed ) $value

Public mixed __get ( string $name  )

Public bool __isset ( string $name  )

Public  void __unset ( string $name )

__set () is called when a value is assigned to an unreachable property.

__get () is called when the value of an inaccessible property is read.

When isset ( ) or empty () is called on an inaccessible property, __isset () is called.

When unset () is called on an inaccessible property, __unset () is called.

The parameter $name refers to the name of the variable to manipulate. The $value parameter of the __set () method Specifies the value of the $name variable.

Property overloading can only be done in an object. In static methods, these magic methods will not be called. So none of These methods can be declared static. Starting with PHP 5.3.0, defining these magic methods as static generates a warning.

Example:

Class a{    public function __set ($name, $value) {         //$this->data[$name]= $value;         $this- > $name = $value;     }    public function __get ($name) {         return  $this $name;    }     public function __isset ($name) {         Return isset ($this-$name);    }    public function  __unset ($name) {        unset ($this, $name);     }} $obj _a=new a (); $obj _a->first= ' first ';echo  $obj _a->first. " <br> "; Echo isset ($obj _a->second)? ' Second exist<br> ': ' second not exist<br> '; Echo isset ($obj _a->first)? ' First exist<br> ': ' first not exist<br> '; Echo empty ($obj _a->first)? ' First exist<br> ': ' first not exist<br> '; unset ($obj _a->first); Echo isset ($obj _a->first)? ' First exist<br> ': ' first not exist<br> ';

Results:

First
second not exist
First exist
First not exist
First not exist

Method overloading

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

Public Static mixed __callstatic ( string , Array) $name $arguments

When an inaccessible method is called in an object, __call () is called.

When a non-accessible method is called in a static manner, __callstatic () is called.

The $name parameter is the name of the method to invoke. The $arguments parameter is an enumerated array that contains the arguments to pass to the method $name.

Example:

echo  "<pre>"; Class a {    public function __call ($name, $ Arguments) {        if ($name = = ' Mycall ') {             print_r ($arguments);             call_user_func (' Mycall ', $arguments [0], $arguments [1]);         }else{            echo  " Call uknow function ";         }    }} Function mycall ($a 1, $a 2) {    echo  "this is mycall<br>";     print_r ($a 1);     print_r ($a 2);    echo  "End  of mycall<br> ";} $obj _a=new a (); $obj _a->mycall ([' a ' = ' + ' aaa '],[' A2 ' + ' A2 ']); $obj _a->notcall ();

Results:

Array ([0] = = Array ([a] = AAA) [1] = = Array ([a2] = A2 ) This is Mycallarray ([a] = AAA) Array ([a2] = A2) End of Mycallcall uknow function

Example:

echo  "<pre>"; class a {    public static function __ Callstatic ($name, $arguments) {        if ($name = = ' Mycall ') {             print_r ($arguments);             call_user_func (' Mycall ', $arguments [0], $arguments [1]);         }else{             echo  "Call uknow function";        }     }}function mycall ($a 1, $a 2) {    echo  "this is mycall<br > ";     print_r ($a 1);     print_r ($a 2);     echo   "End of mycall<br>";} $obj _a=new a (); $obj _a::mycall ([' a ' = ' + ' aaa '],[' A2 ' + ' A2 ']); $obj _a::notcall ();

Results:

Array ([0] = = Array ([a] = AAA) [1] = = Array ([a2] = A2 ) This is Mycallarray ([a] = AAA) Array ([a2] = A2) End of Mycallcall uknow function

PHP Magic Method overloading

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.