Home > Backend Development > PHP Tutorial > --- Section 11--Reloading

--- Section 11--Reloading

WBOY
Release: 2016-07-29 08:34:58
Original
800 people have browsed it

/*
+------------------------------------------------ ----------------------------------+
| = This article is read by Haohappy<> ;
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid unnecessary trouble that may occur, please do not reprint, thank you
| = Welcome to criticize and correct, hope and all PHP enthusiasts Make progress together!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+-------------------------- -------------------------------------------------- ---+
*/
Section 11--Overloading
PHP4 already has overloading syntax to establish mapping to external object models, just like Java and COM. PHP5 brings powerful object-oriented Overloading allows programmers to create custom behaviors to access properties and call methods.
Overloading can be performed through several special methods __get, __set, and __call. When the Zend engine tries to access a member and does not find it, PHP These methods will be called.
In Example 6.14, __get and __set replace all accesses to the attribute variable array. If necessary, you can implement any type of filtering you want. For example, the script can disable setting attribute values ​​at the beginning Use a certain prefix or contain a certain type of value. The __call method explains how you call an undefined method. When you call an undefined method, the method name and the parameters received by the method will be passed to the __call method, and PHP passes_ The value of _call is returned to the undefined method.
Listing 6.14 User-level overloading

Copy the code The code is as follows:

class Overloader
{
private $proper ties = array();
function __get($property_name)                                                                                                                [$property_name]);
                                                                                                      NULL);
                                                                                                                                                                       value;
         }                                                                                                                                ("Invoking $function_name()
n");                                                                                  
}
$o = new Overloader();
//invoke __set() assigns a value to a non-existent attribute variable and activates __set()
$o->dynaProp = "Dynamic Content";
//invoke __get() activates __get()
print($ o->dynaProp . "
n");
//invoke __call() Activate __call()
$o->dynaMethod("Leon", "Zeev");
?>

The above has introduced --- Section 11 -- Overloading, including --- aspects. I hope it will be helpful to friends who are interested in PHP tutorials.


Related labels:
---
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template