PHP pre-defined interface: arrayaccess

Source: Internet
Author: User
Tags mixed

Brief introduction
    • An interface that provides the ability to access an object like an array.
Interface Summary
ArrayAccess {/* 方法 */abstract public boolean offsetExists ( mixed $offset )abstract public mixed offsetGet ( mixed $offset )abstract public void offsetSet ( mixed $offset , mixed $value )abstract public void offsetUnset ( mixed $offset )}
Catalogue
  • arrayaccess::offsetexists-Check if an offset position exists
  • Arrayaccess::offsetget-gets the value of an offset position
  • arrayaccess::offsetset-setting the value of an offset position
  • arrayaccess::offsetunset-resets the value of an offset position

Code Demo

Class OBJ implements arrayaccess{private $container = []; Public Function Offsetexists ($offset): bool {echo ' call '. __method__. ' Method '.        Php_eol;        Echo Print_r (Func_get_args (), true);    return Isset ($this->container[$offset]); The Public Function Offsetget ($offset) {echo ' calls '. __method__. ' Method '.        Php_eol;        Echo Print_r (Func_get_args (), true); return $this->container[$offset]??    Null The Public Function Offsetset ($offset, $value) {echo ' calls '. __method__. ' Method '.        Php_eol;        Echo Print_r (Func_get_args (), true);    $this->container[$offset] = $value; The Public Function Offsetunset ($offset) {echo ' calls '. __method__. ' Method '.        Php_eol;        Echo Print_r (Func_get_args (), true);    unset ($this->container[$offset]); }}//Instantiate Object $zhangsan = new OBJ (),//Assignment $zhangsan[' name '] = ' Zhang San ';//Call Obj::offsetset method//Output echo $zhangsan [' name ']. php_eol;//call Obj::offsetget method//Check for presence Isset ($zhangsan [' name ']) . php_eol;//call obj::offsetexists method//delete array unit unset ($zhangsan [' name ']);//Call Obj::offsetunset method

Application Case Demo

Implementing profile information Reading

Preparatory work:

Create a file according to the directory structure given

./├── config│?? ├── app.php│?? └── database.php└── config.php
    • ./config/app.php

      <?phpreturn [    ‘name‘ => ‘app name‘,    ‘version‘ => ‘v1.0.0‘];
    • ./config/database.php

      <?phpreturn [    ‘mysql‘ => [        ‘host‘ => ‘localhost‘,        ‘user‘ => ‘root‘,        ‘password‘ => ‘12345678‘    ]];
    • ./config.php
<?phpnamespace config;final Class Config implements \arrayaccess{private $config = [];    private static $instance = null;    private $path = null; Private Function __construct () {$this->path = __dir__.    '/config/'; }//Singleton mode gets instance public static function getinstance () {if (!self:: $instance instanceof self) {sel        F:: $instance = new Self;    } return Self:: $instance; }//Prevent being cloned private function __clone () {} public Function offsetexists ($offset) {return isset ($th    is->config[$offset]); Public Function Offsetget ($offset) {if (!isset ($this->config[$offset]) | | empty ($this->config[$OFFSE T])) {//Load profile $this->config[$offset] = require $this->path. $offset.        '. php ';    } return $this->config[$offset];    Public Function Offsetset ($offset, $value) {throw new \exception (' Do not provide provisioning configuration '); } Public Function Offsetunset ($offSet) {throw new \exception (' Do not provide delete configuration '); }} $config = Config::getinstance ();//Get app.php file Nameecho $config [' app '] [' name ']. Php_eol; App name//Gets the user configuration of database.php file MySQL echo $config [' database '] [' MySQL '] [' user ']. Php_eol; Root

PHP pre-defined interface: Arrayaccess

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.