Home Backend Development PHP Tutorial ArrayAccess interface introduction_PHP tutorial

ArrayAccess interface introduction_PHP tutorial

Jul 13, 2016 pm 04:58 PM
php5 introduce exist interface of system translate

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if the translation web page that comes with the Edge browser is missing? What should I do if the translation web page that comes with the Edge browser is missing? Mar 14, 2024 pm 08:50 PM

The edge browser comes with a translation function that allows users to translate anytime and anywhere, which brings great convenience to users. However, many users say that the built-in translation webpage is missing. Then the edge browser automatically What should I do if the translation page I brought is missing? Let this site introduce how to restore the translated web page that comes with the Edge browser if it is missing. How to restore the translation webpage that comes with the Edge browser is missing 1. Check whether the translation function is enabled: In the Edge browser, click the three dots icon in the upper right corner, and then select the "Settings" option. On the left side of the settings page, select the Language option. Make sure "Translate&rd"

Don't worry about watching movies without subtitles! Xiaomi announces the launch of Xiaoai Translation real-time subtitles for Japanese and Korean translation Don't worry about watching movies without subtitles! Xiaomi announces the launch of Xiaoai Translation real-time subtitles for Japanese and Korean translation Jul 22, 2024 pm 02:11 PM

According to news on July 22, today, the official Weibo of Xiaomi ThePaper OS announced that Xiaoai Translation has been upgraded. Real-time subtitles have been added to Japanese and Korean translations, and subtitle-free videos and live conferences can be transcribed and translated in real time. Face-to-face simultaneous interpretation supports translation into 12 languages, including Chinese, English, Japanese, Korean, Russian, Portuguese, Spanish, Italian, French, German, Indonesian, and Hindi. The above functions currently only support the following three new phones: Xiaomi MIX Fold 4 Xiaomi MIX Flip Redmi K70 Extreme Edition It is reported that in 2021, Xiao Ai’s AI subtitles will be added to Japanese and Korean translations. AI subtitles use Xiaomi’s self-developed simultaneous interpretation technology to provide a faster, more stable and accurate subtitle reading experience. 1. According to the official statement, Xiaoai Translator can not only be used in audio and video venues

What are the internal interfaces of a computer motherboard? Recommended introduction to the internal interfaces of a computer motherboard What are the internal interfaces of a computer motherboard? Recommended introduction to the internal interfaces of a computer motherboard Mar 12, 2024 pm 04:34 PM

When we assemble the computer, although the installation process is simple, we often encounter problems in the wiring. Often, users mistakenly plug the power supply line of the CPU radiator into the SYS_FAN. Although the fan can rotate, it may not work when the computer is turned on. There will be an F1 error "CPUFanError", which also causes the CPU cooler to be unable to adjust the speed intelligently. Let's share the common knowledge about the CPU_FAN, SYS_FAN, CHA_FAN, and CPU_OPT interfaces on the computer motherboard. Popular science on the CPU_FAN, SYS_FAN, CHA_FAN, and CPU_OPT interfaces on the computer motherboard 1. CPU_FANCPU_FAN is a dedicated interface for the CPU radiator and works at 12V

How to solve the problem that Google Chrome's built-in translation fails? How to solve the problem that Google Chrome's built-in translation fails? Mar 13, 2024 pm 08:46 PM

Browsers generally have built-in translation functions, so you don’t have to worry about not being able to understand when browsing foreign language websites! Google Chrome is no exception, but some users find that when they open the translation function of Google Chrome, there is no response or failure. What should they do? You can try the latest solution I found. Operation tutorial: Click the three dots in the upper right corner and click Settings. Click Add Language, add English and Chinese, and make the following settings for them. The English setting asks whether to translate web pages in this language. The Chinese setting displays web pages in this language, and Chinese must be moved to the top before it can be set as the default language. If you open the webpage and no translation option pops up, right-click and select Translate Chinese, OK.

Common programming paradigms and design patterns in Go language Common programming paradigms and design patterns in Go language Mar 04, 2024 pm 06:06 PM

As a modern and efficient programming language, Go language has rich programming paradigms and design patterns that can help developers write high-quality, maintainable code. This article will introduce common programming paradigms and design patterns in the Go language and provide specific code examples. 1. Object-oriented programming In the Go language, you can use structures and methods to implement object-oriented programming. By defining a structure and binding methods to the structure, the object-oriented features of data encapsulation and behavior binding can be achieved. packagemaini

Why can't Google Chrome translate Chinese? Why can't Google Chrome translate Chinese? Mar 11, 2024 pm 04:04 PM

Why can't Google Chrome translate Chinese? As we all know, Google Chrome is one of the browsers with built-in translation. When you browse pages written in other countries in this browser, the browser will automatically translate the page into Chinese. Recently, some users have said that they Chinese translation cannot be performed. At this time, we need to fix it in the settings. Next, the editor will bring you the solution to the problem that Google Chrome cannot translate into Chinese. Friends who are interested should come and take a look. Google Chrome cannot translate Chinese solutions 1. Modify the local hosts file. Hosts is a system file without an extension. It can be opened with tools such as Notepad. Its main function is to define the mapping relationship between IP addresses and host names. It is a mapping IP address

Introduction to PHP interfaces and how to define them Introduction to PHP interfaces and how to define them Mar 23, 2024 am 09:00 AM

Introduction to PHP interface and how it is defined. PHP is an open source scripting language widely used in Web development. It is flexible, simple, and powerful. In PHP, an interface is a tool that defines common methods between multiple classes, achieving polymorphism and making code more flexible and reusable. This article will introduce the concept of PHP interfaces and how to define them, and provide specific code examples to demonstrate their usage. 1. PHP interface concept Interface plays an important role in object-oriented programming, defining the class application

Detailed introduction of Samsung S24ai functions Detailed introduction of Samsung S24ai functions Jun 24, 2024 am 11:18 AM

2024 is the first year of AI mobile phones. More and more mobile phones integrate multiple AI functions. Empowered by AI smart technology, our mobile phones can be used more efficiently and conveniently. Recently, the Galaxy S24 series released at the beginning of the year has once again improved its generative AI experience. Let’s take a look at the detailed function introduction below. 1. Generative AI deeply empowers Samsung Galaxy S24 series, which is empowered by Galaxy AI and brings many intelligent applications. These functions are deeply integrated with Samsung One UI6.1, allowing users to have a convenient intelligent experience at any time, significantly improving the performance of mobile phones. Efficiency and convenience of use. The instant search function pioneered by the Galaxy S24 series is one of the highlights. Users only need to press and hold

See all articles