Analysis on the usage of PHP array access interface ArrayAccess, array arrayaccess

Source: Internet
Author: User

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

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.