Home > Backend Development > PHP Tutorial > Section 11 - Overloading - Classes and Objects in PHP5[11]_PHP Tutorial

Section 11 - Overloading - Classes and Objects in PHP5[11]_PHP Tutorial

WBOY
Release: 2016-07-13 16:57:51
Original
881 people have browsed it

| = This article is read by Haohappy<>
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid possible Please do not reprint for unnecessary trouble, thank you
| = Criticisms and corrections are welcome, and we hope to make progress together with all PHP enthusiasts!
+------------------- -------------------------------------------------- ----------+
*/
Section 11--Overloading
PHP4 already has overloading syntax to establish mapping to external object models, like Like Java and COM. PHP5 brings powerful object-oriented overloading, allowing programmers to create custom behaviors to access properties and call methods.
Overloading can be accomplished through several special methods __get, __set, and __call Proceed. PHP will call these methods when the Zend engine tries to access a member and cannot find it.
In Example 6.14, __get and __set replace all accesses to the attribute variable array. You can implement any type if necessary Filter the way you want. For example, a script can disable setting property values, starting with a certain prefix or containing a certain type of value. The
__call method illustrates how you call an undefined method. You call an undefined method When, the method name and parameters received by the method will be passed to the __call method, and PHP returns the value of __call to the undefined method.
Listing 6.14 User-level overloading
class Overloader
{
private $properties = array();
function __get($property_name)
{
if(isset($this->properties[$property_name]))
{
return($this->properties[$property_name]);
}
else
{
return(NULL);
}
}
function __set($property_name, $value)
{
$this->properties[$property_name] = $value;
}
function __call($function_name, $args)
{
print("Invoking $function_name()
n");
print("Arguments: ");
print_r($args);
return(TRUE);
}
}
$o = new Overloader();
//invoke __set() Assign a value to a non-existent attribute variable and activate __set()
$o->dynaProp = "Dynamic Content";
//invoke __get() activates __get()
print($o->dynaProp . "
n");
//invoke __call() activates __call ()
$o->dynaMethod("Leon", "Zeev");
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631483.htmlTechArticle| = This article is for Haohappy reading Core PHP Programming | = Notes from the Chapter Classes and Objects | = Mainly translated +Personal Thoughts| = Please do not reprint to avoid unnecessary trouble that may occur, thank you...
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