Home  >  Article  >  Backend Development  >  ArrayAccess interface introduction_PHP tutorial

ArrayAccess interface introduction_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:58:42928browse

There are a series of new interfaces in PHP5. You can learn about their applications in this series of articles translated by HaoHappy. At the same time, these interfaces and some implemented Classes are classified into the Standard PHP Library (SPL). Many features have been added to PHP5 to further enhance class overloading. The purpose of ArrayAccess is to make your Class look like an array (PHP's array). This is very similar to C#'s Index feature.

Here is the definition of ArrayAccess:

interface ArrayAccess
boolean offsetExists($index)
mixed offsetGet($index)
void offsetSet($index, $newvalue)
void offsetUnset($index)

Due to the power of PHP arrays, many people often save configuration information in an array when writing PHP applications. So there may be globals everywhere in the code. Let’s try another approach?

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 might 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
$config2 = Configuration::instance();
print $config2['Jasmin'];

Yes, as expected, the output will be Binzy's Lover.
You may ask, what is the difference between this and using an array? There is no difference in purpose, but the biggest difference is encapsulation. The most basic job of OO is encapsulation, and encapsulation can effectively keep changes internally. That is, when the configuration information is no longer stored in a PHP array, yes, no changes are required in the application code. What may be done is just to add a new strategy (Strategy) to the configuration scheme. :

ArrayAccess is being further improved, because now there is no way to count, although in most cases it does not affect our use.

Reference:
1. 《PHP5 Power Programming》
2. "Design Pattern"
3. "Object-oriented Analysis and Design"

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631387.htmlTechArticleThere are a series of new interfaces in PHP5. You can learn about their applications in this series of articles translated by HaoHappy. At the same time, these interfaces and some implemented Classes are classified as Standard PHP L...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn