this article illustrates the use of the PHP array access interface ArrayAccess. Share to you for your reference, as follows:

PHP ArrayAccess interface called array access interface, the interface is to provide the ability to access the same object as access array.

 ArrayAccess interface as follows: {/ / get an offset value Abstract public mixed offsetGet (mixed $offset) / / set a setwindowlongptr Abstract public void offsetSet (mixed $offset, mixed $value) / / check if a abstract public Boolean offsetExists offset (mixed $offset) / / reset offset value Abstract public void offsetUnset (mixed $offset) 

} example:

 < PHP? / * * * ArrayAndObjectAccess * to the class allows an array or object to access * * @author * class ArrayAndObjectAccess implements crazy old driver ArrayAccess {/ * * * definition of an array used to store data * * @access private * @var array * / P Rivate $data = []; / * * * object to access the data in an array of public * * * @access @param string * public function __get array element name ($key) {return $this-> data[$key];} / * * * object to add a public * * * @access array element @ param string * @param mixed array array element name * @return mixed * / public element value of function __set ($key, $value) {$this-> data[$key] = $value;} / * * * to determine whether to set the object array elements * * @access public * @param * @return * Boolean array element name public function __isset ($key return) {isset ($this-> data[$key]);} / * * * object delete one of the array elements * * @access Pub LIC * @param * public function __unset the key elements of the array ($key) {unset ($this-> data[$key]);} / * * * to array data array to add an element of * * @access public * @abstracting ArrayAccess * @param string * @param mixed offset value / public function offsetSet elements ($offset, $value) {if (is_null ($offset)) {$this-> data[] = $value;} else {$this-> data[$offset] = $value;}} / * * * to get data array specified array elements public * * * @access position @abstracting ArrayAccess * @param * @return * public mixed offset function offsetGet ($offset) {return $this-> offsetExists ($offset) $this->? Data[$offset]; null;} To judge array offset / * * * * * @access set position element is public * @abstracting ArrayAccess * @param * @return * public Boolean offset function offsetExists ($offset return) {isset ($this-> data[$offset]);} / * * * to delete the specified array of data type array element position * * @access public * @abstracting ArrayAccess * @param offset the position of function offsetUnset / public ($offset) {if ($this-> offsetExists ($offset) unset ($this-> data[$offset]) {}}}); $animal = new (ArrayAndObjectAccess); $animal-> dog ='dog'; $animal['pig'] = __set / / call ArrayAndObjectAccess::'pig': offsetSet var_dump; / / call ArrayAndObjectAccess: (isset ($animal-> dog)); / / call Arr AyAndObjectAccess:: __isset var_dump (isset ($animal['pig'])); / / call ArrayAndObjectAccess:: offsetExists var_dump ($animal-> pig); / / call ArrayAndObjectAccess:: __get var_dump ($animal['dog']); / / call ArrayAndObjectAccess:: offsetGet unset ($animal['dog']); / / call ArrayAndObjectAccess:: offsetUnset unset ($animal-> pig); / / call ArrayAndObjectAccess:: __unset var_dump ($animal['pig']) call ArrayAndObjectAccess:: offsetGet; / / var_dump ($animal-> dog); / / call ArrayAndObjectAccess:: __get? > 

 Boolean true output: Boolean true string'pig'(length=3) string'dog' (length=3) null null 

more PHP on the relevant content of interested readers can see the topic: "PHP array (Array). The skills of "PHP commonly used traversal algorithm and skills summary "," php string (string) "php commonly used functions and skills summary "," PHP error and exception handling methods "," PHP "basic grammar tutorial" php object-oriented programming tutorial ", php+mysql " database entry to teach history and the" php "common database operation skills summary

hopes the PHP program designed to help all of you.


This concludes the body part