zoukankan      html  css  js  c++  java
  • 数组访问-ArrayAccess

    以前对ArrayAccess不是很熟悉,现在整理下下有关ArrayAccess相关的知识,ArrayAccess接口就是提供像访问数组一样访问对象的能力的接口。

    接口内容如下:(PHP自含此接口类,可通过手册查看,配置文件直接读取即可)

    1 interface ArrayAccess{
    2     public function offsetExists($offset);
    3     public function offsetGet($offset);
    4     public function offsetSet($offset,$value);
    5     public function offsetUnset($offset);
    6 }

    配置程序:

    我们可以通过ArrayAccess利用配置文件来控制程序。

    1. 在项目更目录下创建一个config目录 
    2. config目录下创建相应的配置文件,比如app.php database.php。文件程序如下

    app.php

     1 <?php
     2 
     3 return [
     4     'name' => 'app name',
     5     'version' => 'v1.0.0'
     6 ];
     7 db.php
     8 <?php
     9 
    10 return [
    11     'mysql' => [
    12         'host' => 'localhost',
    13         'user' => 'root',
    14         'password' => '12345678'
    15     ]
    16 ];

    3. Config.php实现ArrayAccess

     1 <?php
     2 
     3 namespace Config;
     4 class Config implements ArrayAccess {
     5     private $config = array();
     6     private static $instance;
     7     private $_path = '';
     8 
     9     public function __construct()
    10     {
    11         //初始配置路径
    12         $this->_path = __DIR__.'/../config/';
    13     }
    14 
    15     /**
    16      * 实现实例化自身对象功能
    17      * @return Config
    18      */
    19     public static function instance(){
    20         if (!(self::$instance instanceof Config)) {
    21             self::$instance = new Config();
    22         }
    23         return self::$instance;
    24     }
    25 
    26     /**
    27      * 设置
    28      * @param mixed $key
    29      * @param mixed $val
    30      */
    31     public function offsetSet($key, $val)
    32     {
    33         $this->config[$key] = $val;
    34     }
    35 
    36     /**
    37      * 判断key在不在配置文件数组
    38      * @param mixed $key
    39      * @param mixed $val
    40      */
    41     public function offsetExists($key)
    42     {
    43         return isset($this->config[$key]);
    44     }
    45 
    46     /**
    47      * 销毁配置内某个key
    48      * @param mixed $key
    49      */
    50     public function offsetUnset($key)
    51     {
    52         unset($this->config[$key]);
    53     }
    54 
    55     /**
    56      * 获取数据值得时候加载
    57      * @param mixed $key
    58      * @return mixed|null
    59      */
    60     public function offsetGet($key)
    61     {
    62         if (empty($this->config[$key])) {
    63             $this->config[$key] = require $this->_path.$key.".php";
    64         }
    65 
    66         return isset($this->config[$key]) ? $this->config[$key] : null;
    67     }
    68 }
    69 
    70 
    71 //入口配置文件 读取
    72 
    73 include '../app/model/Config.php';
    74 $config = Config::instance();
    75 var_dump($config['db']['mysql']);
    76 exit;
  • 相关阅读:
    mac上配置apidoc环境
    Yii 2.0 GII 访问404错误
    yii框架下使用redis
    PHPEXCEL读出数据是PHPExcel_RichText类型
    php实现仿百度分页
    apache的.htaccess文件作用和相关配置
    Yii2.0 解决“the requested URL was not found on this server”问题
    [基础与理论]C#变量
    基础&理论]C#的基本语法
    [基础&理论]C#的含义
  • 原文地址:https://www.cnblogs.com/BrokenHeart/p/10681340.html
Copyright © 2011-2022 走看看