Analysis on the usage of PHP array access interface ArrayAccess, array arrayaccess
This example describes how to use the PHP array access interface ArrayAccess. We will share this with you for your reference. The details are as follows:
The PHP ArrayAccess interface is also called an array access interface, which provides the ability to access objects like an array.
The interface abstract is as follows:
ArrayAccess {// obtain the value of an offset position abstract public mixed offsetGet (mixed $ offset) // set the value of an offset position abstract public void offsetSet (mixed $ offset, mixed $ value) // check whether a specified offset exists. abstract public boolean offsetExists (mixed $ offset) // reset the value of an offset. abstract public void offsetUnset (mixed $ offset )}
Example:
<? Php/*** ArrayAndObjectAccess * THIS class allows access in array or object mode ** @ author crazy old driver */class ArrayAndObjectAccess implements ArrayAccess {/*** defines an array for saving data ** @ access private * @ var array */private $ data = []; /*** access data in the array as an object ** @ access public * @ param string array element key name */public function _ get ($ key) {return $ this-> data [$ key];} /*** add an array element as an object ** @ access public * @ param string array element key name * @ param mixed Array Element value *@ Return mixed */public function _ set ($ key, $ value) {$ this-> data [$ key] = $ value ;} /*** determine whether to set the array element as an object ** @ access public * @ param array element key name * @ return boolean */public function _ isset ($ key) {return isset ($ this-> data [$ key]);} /*** delete an array element as an object ** @ access public * @ param array element key name */public function _ unset ($ key) {unset ($ this-> data [$ key]);}/*** add an element to the data array as an array ** @ access public * @ abstrac Ting ArrayAccess * @ param string offset position * @ param mixed Element value */public function offsetSet ($ offset, $ value) {if (is_null ($ offset )) {$ this-> data [] = $ value;} else {$ this-> data [$ offset] = $ value ;}} /*** obtain the specified position element of the data array in array mode ** @ access public * @ brief acting ArrayAccess * @ param offset position * @ return mixed */public function offsetGet ($ offset) {return $ this-> offsetExists ($ offset )? $ This-> data [$ offset]: null ;} /*** determine whether to set the offset position element in array mode ** @ access public * @ brief acting ArrayAccess * @ param offset position * @ return boolean */public function offsetExists ($ offset) {return isset ($ this-> data [$ offset]);} /*** Delete the specified position element of the data array in array mode ** @ access public * @ brief acting ArrayAccess * @ param offset position */public function offsetUnset ($ offset) {if ($ this-> offsetExists ($ offset) {unset ($ this-> data [$ off Set]) ;}}$ animal = new ArrayAndObjectAccess (); $ animal-> dog = 'Dog'; // call ArrayAndObjectAccess :: __set $ animal ['pig'] = 'pig'; // call ArrayAndObjectAccess: offsetSetvar_dump (isset ($ animal-> dog); // call ArrayAndObjectAccess :: __issetvar_dump (isset ($ animal ['pig']); // call ArrayAndObjectAccess: offsetExistsvar_dump ($ animal-> pig); // call ArrayAndObjectAccess :: __getvar_dump ($ animal ['Dog']); // call ArrayAndObject Access: offsetGetunset ($ animal ['dog ']); // call ArrayAndObjectAccess: offsetUnsetunset ($ animal-> pig); // call ArrayAndObjectAccess :: __unsetvar_dump ($ animal ['pig']); // call ArrayAndObjectAccess: offsetGetvar_dump ($ animal-> dog); // call ArrayAndObjectAccess :__ get?>
The above output:
boolean trueboolean truestring 'pig' (length=3)string 'dog' (length=3)nullnull