Home > Backend Development > PHP Tutorial > PHP5.0 Object Model Exploration Overloading_PHP Tutorial

PHP5.0 Object Model Exploration Overloading_PHP Tutorial

WBOY
Release: 2016-07-13 17:21:32
Original
720 people have browsed it

PHP4 already has overloading syntax to establish mappings to external object models, just 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 performed through several special methods __get, __set, and __call. When the Zend engine tries to access a member and cannot find it, PHP will call these methods.

In In the following example, __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, start with a certain prefix or include A value of a certain type.

The __call method describes 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 __call Value returned to undefined method.

Listing1 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);
} properties[$property_name] = $value; print("Arguments: ");
print_r($args);

return(TRUE);
}
}
$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");
?>





http://www.bkjia.com/PHPjc/532421.html

www.bkjia.com

true
http: //www.bkjia.com/PHPjc/532421.html

TechArticle

PHP4 already has overloaded syntax to establish mapping to external object models, just like Java and COM. .PHP5 brings powerful object-oriented overloading, allowing programmers to create custom behaviors to...
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