Introduction to PHP Arrayaccess interface

Source: Internet
Author: User

There are a number of new interfaces in PHP5. In Haohappy translations you can learn about their applications. At the same time, these interfaces and some of the implemented classes are classified as standard PHP Library (SPL). A number of features have been added to the PHP5 to further enhance the overloading of classes (overloading). Arrayaccess's role is to make your Class look like an array (an array of PHP). This is similar to the Index feature of C #.

Here is the definition of arrayaccess:

Interface Arrayaccess

    • Boolean offsetexists ($index)

    • Mixed Offsetget ($index)

    • void Offsetset ($index, $newvalue)

    • void Offsetunset ($index)

Because of the power of the PHP array, many people often keep configuration information in an array when writing PHP applications. So it's possible that global is everywhere in the code. How do we change?

classConfigurationImplementsarrayaccess {Static Private $config; Private $configarray; Private function__construct () {//Init        $this->configarray =Array("Binzy" = "Male", "Jasmin" = "Female"); }     Public Static functioninstance () {//        if(Self::$config==NULL) { self::$config=NewConfiguration (); }        returnSelf::$config; }    //Check if an offset position exists    functionOffsetexists ($index) {        return isset($this->configarray[$index]); }    //gets the value of an offset position    functionOffsetget ($index) {        return $this->configarray[$index]; }    //sets the value of an offset position    functionOffsetset ($index,$newvalue) {        $this->configarray[$index] =$newvalue; }    //resets the value of an offset position    functionOffsetunset ($index) {        unset($this->configarray[$index]); }}$config= Configuration::instance ();Print_r($config);Echo"<br/>";Echo $config[' Binzy '];Echo"<br/>";$config[' binzy '] = ' 1222 ';Echo $config[' Binzy '];

Introduction to PHP Arrayaccess interface

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.