Section 11--Reloading_PHP Tutorial

WBOY
Release: 2016-07-21 16:01:06
Original
927 people have browsed it

/*
+-------------------------------------------------- ----------------------------------+
| = This article is read by Haohappy<>
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid possible unnecessary trouble, please do not reprint, thank you
| = We welcome criticisms and corrections, and hope to make progress together with all PHP enthusiasts!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+---------- -------------------------------------------------- ------------------+
*/
Section 11--Overloading
There is already overloading syntax in PHP4 to create Mapping of external object models, just like Java and COM. PHP5 brings powerful object-oriented overloading, allowing programmers to build custom behaviors to access properties and call methods.
Overloading can be done through __get, __set, and __call are several special methods. When the Zend engine tries to access a member and cannot find it, PHP will call these methods.
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 property values, start with a certain prefix or include a certain type of value. The
__call method illustrates how you can call a property without Defined 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 returns the value of __call to the undefined method.
Listing 6.14 User-level overloading

Copy code The code is as follows:
class Overloader
{
private $properties = array();
function __get($property_name)
{ Return($this->properties[$property_name ]);                                                                                                                                                                                                                                             $this->properties[$property_name] = $value;                                           gt;n ");
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");
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316934.htmlTechArticle/* +--------------------- -------------------------------------------------- --------+ |=This article is Haohappy's notes from the chapter ClassesandObjects when reading CorePHP Programming |=Translation is mainly + personal...
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