Php5 learning notes (2)

Source: Internet
Author: User
Php 5 learning record (2) I. reload performance 1. reload of attributes and methods php allows reload of attribute access and method calls by implementing special proxy methods, these proxy methods will be called when the relevant property or method does not exist. This feature allows you to gain great flexibility when you perform these actions in the middle and define your own features. You can implement the following method prototype: function _ php5 learning record (2)

I. heavy load performance

1. overload of attributes and methods

Php allows you to implement special proxy methods to overload access to attributes and call methods. these proxy methods are called when the relevant attributes or methods do not exist. This feature allows you to gain great flexibility when you perform these actions in the middle and define your own features.

You can implement the following method prototype:

Function _ get ($ property) // pass the attribute name and return the attribute value

Function _ set ($ property, $ value) // pass the attribute name and the new value

Function _ call ($ method, $ args) // The name of the transfer method and an array of numeric indexes. The number contains the passed parameters. The index of the first parameter is 0.

?

?

class Users {        private $arr = array();    function __get($name) {        return $this->arr[$name] === NULL ? 'null' : $this->arr[$name];    }    function __set($name, $value) {        $this->arr[$name] = $value;    }}$user = new Users();$user->a = 'xiezheng';print $user->a;

_ Call () is used to monitor other methods in an object. If you try to call a method that does not exist in an object, the __call method will be automatically called.

?

class HelloWorld {    function display($count) {        for($i=0; $i<$count; $i++)            print __CLASS__.'
'; return $count; }}class HelloWorldCall { private $obj; function __construct() { $this->obj = new HelloWorld(); } function __call($method, $args) { return call_user_func_array(array($this->obj, $method), $args); }}$hwc = new HelloWorldCall();print $hwc->display(3);
?

?

2. use the array statement to Access the overload.

To enable your class to access the array statements with heavy loads, your class needs to implement the ArrayAccess interface.

Bool offsetExists ($ index)

Mined offsetGet ($ index)

Void offsetSet ($ index, $ new_value)

Void offsetUnset ($ index)

The above is the implementation method required by ArrayAccess.

?

?

class Users implements ArrayAccess {        private $uname;    function offsetExists($name) {        return $this->uname === NULL;    }    function offsetGet($name) {        return $this->offsetExists($this->uname) ? 'NULL' : $this->uname;    }    function offsetSet($name, $value) {        $this->uname = $value;    }    function offsetUnset($name) {        $this->uname = null;    }}$user = new Users();$user['name'] = 'xiezheng';// unset($user['name']);print $user['name'];
?

?

?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.