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'];
?
?
?