


Section 11--Overloading--ClassesandObjectsinPHP511_PHP Tutorial
Jul 13, 2016 pm 05:24 PM
| = This article is read by Haohappy > | = Notes from the Chapter Classes and Objects | = Translation + personal experience | = Please do not reprint to avoid unnecessary trouble that may occur, thank you | = Welcome to criticize and correct me, Hope to make progress together with all PHP enthusiasts! +------------------------------------------------ ---------------------------------------+ */ Section 11--Reloading 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 done through the special methods __get, __set, and __call. 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 pairs. Access to an array of attribute variables. If necessary, you can implement any type of filtering you want. For example, the script can prohibit setting attribute values, start with a certain prefix or include a certain type of value. The __call method illustrates how you call 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 returns the value of __call to the undefined method. Listing 6.14 User-level overloading 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() 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"); ?>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon
