How to use the arrayaccess of PHP pre-defined interface

Source: Internet
Author: User
After PHP5, a series of predefined interfaces are added, collectively known as SPL, for these interfaces and implemented classes. These interfaces have a heavyweight interface arrayaccess, which is used to make your object accessible like an array.

interface introduction, using the Arrayaccess method requires the definition of four methods.

arrayaccess {    /* Methods * *    Abstract public boolean offsetexists (mixed $offset)    abstract public mixed OFFSE Tget (mixed $offset)    abstract public void Offsetset (mixed $offset, mixed $value)    abstract public void offset Unset (mixed $offset)}

The specific implementation of the interface in Slim is given below. A collection class collection is defined in slim, which provides a common interface method for collection objects. Where this class inherits the Collectioninterface interface, it inherits the Arrayaccess predefined interface.

/*** the collection interface, passing in an array in the container's settings, returning a collection object. * Collection interfacecollectioninterface* @package slim* @since   3.0.0 */interface collectioninterface extends \ Arrayaccess, \countable, \iteratoraggregate{public       function set ($key, $value);    Public function Get ($key, $default = null);    Public function replace (array $items);    Public function All ();    Public function has ($key);    Public function Remove ($key);    Public function Clear ();} Say, slim framework of the collection class. After merging the container's default configuration $defaultsetting (private variable of the class) and the User Configuration $usersetting (log, template, database configuration, etc.) in Slim container Contianer, a Collection object collection is returned. Since the collection object inherits Arrayaccess, we can manipulate the configuration file as if it were an array after loading it sequentially.

This article explains the PHP pre-defined interface of the use of arrayaccess, this method in PHP has an important role, more relevant content please pay attention to the PHP Chinese web.

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.