Arrayaccess Interface Introduction _php Tutorial

Source: Internet
Author: User
There are a number of new interfaces in PHP5. You can learn about their applications in the series of articles translated by Haohappy. 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?

such as 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 can expect, the output of the program is "Male".
If we do the following actions:

$config = Configuration::instance ();
Print $config ["Binzy"];
$config [' Jasmin '] = "Binzy ' s Lover";
Config 2
$config 2 = configuration::instance ();
Print $config 2[' Jasmin '];

Yes, and as expected, the output will be Binzy ' s Lover.
Perhaps you would ask, what is the difference between this and using arrays? The goal is no different, but the biggest difference is in encapsulation. The most basic work of OO is encapsulation, and encapsulation can effectively put the change 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. The only possible thing to do is to add a new policy (strategy) for the configuration scenario. :

Arrayaccess in the further refinement, because there is no way to count, although most of the situation does not affect our use.

Reference:
1. "PHP5 Power Programming"
2. "Design mode"
3. "Object-oriented analysis and design"

http://www.bkjia.com/PHPjc/631387.html www.bkjia.com true http://www.bkjia.com/PHPjc/631387.html techarticle There are a number of new interfaces in PHP5. You can learn about their applications in the series of articles translated by Haohappy. At the same time these interfaces and some of the implementation of the Class 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.