加入收藏 | 设为首页 | 会员中心 | 我要投稿 无锡站长网 (https://www.0510zz.cn/)- 运维、开发、CDN、操作系统、语音技术!
当前位置: 首页 > 教程 > 正文

PHP数组式访问-ArrayAccess示例解析

发布时间:2022-07-25 10:03:48 所属栏目:教程 来源:互联网
导读:本文章主要讲述了PHP中的数组式访问,具有一定参考价值,感兴趣的朋友可以了解一下,希望能帮助到你。 以前对ArrayAccess不是很熟悉,现在整理下下有关ArrayAccess相关的知识,ArrayAccess接口就是提供像访问数组一样访问对象的能力的接口。 接口内容如下:

   
          if (!(self::$instance instanceof Config)) {
   
              self::$instance = new Config();
   
          }
   
          return self::$instance;
   
      }
   
        
   
      public function offsetExists($offset)
   
      {
   
          return isset($this->config[$offset]);
   
      }
   
        
   
      public function offsetGet($offset)
   
      {
   
          if (emptyempty($this->config[$offset])) {
   
              $this->config[$offset] = require $this->path.$offset.".php";
   
          }
   
          return $this->config[$offset];
   
      }
   
   
   
      public function offsetSet($offset, $value)
   
      {
   
          throw new Exception('不提供设置配置');
   
      }
   
   
   
      public function offsetUnset($offset)
   
      {
   
          throw new Exception('不提供删除配置');
   
      }
   
  }
   
   
   
  $config = Config::instance();
   
   
   
  //获取app.php 文件的 name
   
  echo $config['app']['name'].PHP_EOL; //app name
   
   
   
  //获取database.php文件mysql的user配置
   
  echo $config['database']['mysql']['user'].PHP_EOL; // root 

(编辑:无锡站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读