"PHP Classes and objects" overload

Source: Internet
Author: User
Tags php class

This article to share the content is about the "PHP class and Object" overload, has a certain reference value, the need for friends can refer to

Overloading (overloading)

Definition: Refers to the dynamic creation of class properties and methods.

Implementation: Magic method.

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 public

None of the parameters of these magic methods can be passed by reference.

Property overloading

The public void __set (string $name, mixed $value) is called when a value is assigned to an inaccessible property __set (). When public mixed __get (string $name) reads the value of an inaccessible property, __get () is called. public bool __isset (string $name) when calling Isset () or empty on an inaccessible property (), __isset () is called. public void __unset (string $name) when unset () is called on an inaccessible property, __unset () is called.

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

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

 "not understood" cannot use overloaded properties in other language constructs other than isset (), which means that overloaded magic methods will not be called when empty () is used on an overloaded property. To circumvent this restriction, you must assign an overloaded property to a local variable and then use empty (). 
Example #1 use __get (), __set (), __isset (), and __unset () for property overloading <?phpclass PropertyTest {/** The overloaded data is saved in this */private     $data = Array ();     /** overloads cannot be used on properties that have already been defined */public $declared = 1;    /** The overload occurs only if this property is accessed from outside the class */private $hidden = 2;        Public Function __set ($name, $value) {echo ' Setting ' $name ' to ' $value ' \ n ';    $this->data[$name] = $value;        } Public Function __get ($name) {echo ' Getting ' $name ' \ n ';        if (Array_key_exists ($name, $this->data)) {return $this->data[$name];        } $trace = Debug_backtrace ();            Trigger_error (' Undefined property via __get (): '. $name. ' In '.            $trace [0][' file ']. ' On line '.        $trace [0][' line '], e_user_notice);    return null;        }/** PHP 5.1.0 after version */Public function __isset ($name) {echo "is ' $name ' set?\n";    return Isset ($this->data[$name]); /** PHP 5.1.0 after version */Public Function __unset ($name) {echo ' unsetting ' $name ' \ n ';    unset ($this->data[$name]);    }/** Non-magic method */Public Function Gethidden () {return $this->hidden;   }}echo "<pre>\n"; $obj = new PropertyTest; $obj->a = 1; __set (): Setting ' A ' to ' 1 ' echo $obj->a.     "\ n";  __get (): Getting ' A ' Var_dump (isset ($obj->a)); __isset (): Is ' a ' set?        BOOL (TRUE) unset ($obj->a);  __unset (): unsetting ' A ' Var_dump (isset ($obj->a)); __isset (): Is ' a ' set? BOOL (false) echo "\ n"; Echo $obj->declared.  "\ n"; Accessible, __get () not called: 1echo "Let's experiment with the private property named ' Hidden ': \ n"; echo "privates is visible inside t He class, so __get () is not used...\n "; Echo $obj->gethidden (). "\ n"; Accessible, __get () not called: 2echo "privates not visible outside of class, so __get () is used...\n"; Echo $obj->hidden.  "\ n"; __get (): Notice:undefined property via __get (): Hidden in D:\phpStudy2018\PHPTutorial\WWW\index.php on line D:\phpStudy2018\PHPTutorial\WWW\index.php on line 31?> 

Method overloading

When public mixed __call (string $name, array $arguments) calls an inaccessible method in an object, __call () is called. public static mixed __callstatic (string $name, array $arguments) is called when a non-accessible method is called in the static Context __callstatic ().

Related recommendations:

PHP in the overload of the detailed

Introduction to PHP 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.