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