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