ArrayAccess interface introduction. A series of new interfaces are added in PHP5. You can learn about their applications in the series of articles translated by HaoHappy. At the same time, these interfaces and some implemented classes are classified as StandardPHPL and a series of new interfaces are added in PHP5. You can learn about their applications in the series of articles translated by HaoHappy. These interfaces and some implemented classes are classified as Standard PHP Library (SPL ). Many features are added to PHP5 to further enhance the Overloading of classes. ArrayAccess is used to make your Class look like an array (PHP array ). This is similar to the Index feature of C.
The following is the definition of ArrayAccess:
Interface ArrayAccess
Boolean offsetExists ($ index)
Mixed offsetGet ($ index)
Void offsetSet ($ index, $ newvalue)
Void offsetUnset ($ index)
Due to the powerful PHP array, many users often save configuration information in an array when writing PHP applications. Therefore, global may be everywhere in the code. In another way?
Run the following code:
// Configuration Class
Class Configuration implements ArrayAccess
{
Static private $ config;
Private $ configarray;
Private function _ construct ()
{
// Init
$ This-> configarray = array ("Binzy" => "Male", "Jasmin" => "Female ");
}
Public static function instance ()
{
//
If (self: $ config = null)
{
Self: $ config = new Configuration ();
}
Return self: $ config;
}
Function offsetExists ($ index)
{
Return isset ($ this-> configarray [$ index]);
}
Function offsetGet ($ index ){
Return $ this-> configarray [$ index];
}
Function offsetSet ($ index, $ newvalue ){
$ This-> configarray [$ index] = $ newvalue;
}
Function offsetUnset ($ index ){
Unset ($ this-> configarray [$ index]);
}
}
$ Config = Configuration: instance ();
Print $ config ["Binzy"];
As you expected, the program output is "Male ".
Suppose we do the following action:
$ Config = Configuration: instance ();
Print $ config ["Binzy"];
$ Config ['jasmin'] = "Binzy's Lover ";
// Config 2
$ Config2 = Configuration: instance ();
Print $ config2 ['jasmin'];
Yes, as expected, the output will be Binzy's Lover.
Maybe you will ask, what is the difference between this and using arrays? There is no difference in purpose, but the biggest difference is encapsulation. The most basic task of OO is encapsulation, which can effectively put changes inside. That is, when the configuration information is no longer stored in a PHP array, yes, the application code does not need to be changed. You may only need to add a new Strategy for the configuration scheme ). :
ArrayAccess is being further improved, because there is no way to count, although in most cases it does not affect our use.
Refer:
1. PHP5 Power Programming
2. design patterns
3. object-oriented analysis and design
PHP5 has a series of new interfaces. You can learn about their applications in the series of articles translated by HaoHappy. At the same time, these interfaces and some implemented classes are classified as Standard php l...