ArrayAccess interface introduction _ PHP Tutorial

Source: Internet
Author: User
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...

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.