当前位置:首页 > PHP教程 > php数组 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2020-04-04 21:44:12 浏览: 评论:0 

本文章主要讲述了PHP中的数组式访问,具有一定参考价值,感兴趣的朋友可以了解一下,希望能帮助到你。

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

接口内容如下:

  1. ArrayAccess { 
  2.  
  3.     //检查一个偏移位置是否存在  
  4.  
  5.     abstract public boolean offsetExists ( mixed $offset );  
  6.  
  7.     //获取一个偏移位置的值  
  8.  
  9.     abstract public mixed offsetGet ( mixed $offset );  
  10.  
  11.     //设置一个偏移位置的值  
  12.  
  13.     abstract public void offsetSet ( mixed $offset , mixed $value );  
  14.  
  15.     //复位一个偏移位置的值  
  16.  
  17.     abstract public void offsetUnset ( mixed $offset );  
  18.  
  19. } 

项目中使用,获取网站配置:

  1. <?php 
  2.  
  3. namespace lib; 
  4.  
  5. use mpf\core\Di; 
  6.  
  7. class config implements \ArrayAccess{ 
  8.  
  9. //定义存储数据的数组 
  10.  
  11.    protected $configs; 
  12.  
  13.    public function __construct($configs){ 
  14.  
  15.          $this->configs = $configs; 
  16.  
  17.          $configs = \lib\model\Home::getWebConfig(); 
  18.  
  19.          foreach( $configs as $config ){ 
  20.  
  21.                if( !isset($this->configs[$config['sc_key']]) ){ 
  22.  
  23.                    $this->configs[$config['sc_key']] = $config['sc_content']; 
  24.  
  25.                } 
  26.  
  27.          } 
  28.  
  29.    } 
  30.  
  31.    public function get($key){ 
  32.  
  33.          if( isset($this->configs[$key]) ){ 
  34.  
  35.                return $this->configs[$key]; 
  36.  
  37.          }elseif( $key == 'caipiao'){ 
  38.  
  39.                $this->configs['caipiao'] = \lib\model\Home::getLcs();   
  40.  
  41.                return $this->configs[$key]; 
  42.  
  43.          }elseif( $key == 'user_money' ){ 
  44.  
  45.                if( isset($_SESSION['uid']) ){ 
  46.  
  47.                  if( $_SESSION['utype'] == 5 ){ 
  48.  
  49.                        $sql = 'select money from inner_user where uid=?'; 
  50.  
  51.                  }else{ 
  52.  
  53.                        $sql = 'select money from user where uid=?'; 
  54.  
  55.                  } 
  56.  
  57.                    $this->configs['user_money'] = \mpf\core\Di::$Di->db->prepare_query($sql,[getUid()])->fetch(\PDO::FETCH_COLUMN); 
  58.  
  59.                    return $this->configs['user_money']; 
  60.  
  61.              } 
  62.  
  63.        } 
  64.  
  65.    } 
  66.  
  67.    public function offsetExists($index){ 
  68.  
  69.          return isset($this->configs[$index]); 
  70.  
  71.    } 
  72.  
  73.    public function offsetGet($index){ 
  74.  
  75.          return $this->configs[$index]; 
  76.  
  77.    } 
  78.  
  79.    public function offsetSet($index,$val){ 
  80.  
  81.          $this->configs[$index] = $val; 
  82.  
  83.    } 
  84.  
  85.    public function offsetUnset($index){ 
  86.  
  87.          unset($this->configs[$index]); 
  88.  
  89.    } 
  90.  
  91. } 

这样可以使用config对象来直接访问配置信息内容。

配置程序:

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

1. 在项目更目录下创建一个config目录

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

app.php

  1. <?phpreturn [    
  2.  
  3.  'name' => 'app name',    
  4.  
  5.   'version' => 'v1.0.0' 
  6.  
  7. ]; 

database.php

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

3. Config.php实现ArrayAccess

  1. <?php 
  2.  
  3. namespace Config; 
  4.  
  5. class Config implements \ArrayAccess 
  6.  
  7. { 
  8.  
  9.     private $config = []; 
  10.  
  11.  
  12.  
  13.     private static $instance; 
  14.  
  15.  
  16.  
  17.     private $path; 
  18.  
  19.  
  20.  
  21.     private function __construct() 
  22.  
  23.     { 
  24.  
  25.         $this->path = __DIR__."/config/"; 
  26.  
  27.     } 
  28.  
  29.  
  30.  
  31.     public static function instance() 
  32.  
  33.     { 
  34.  
  35.         if (!(self::$instance instanceof Config)) { 
  36.  
  37.             self::$instance = new Config(); 
  38.  
  39.         } 
  40.  
  41.         return self::$instance; 
  42.  
  43.     } 
  44.  
  45.       
  46.  
  47.     public function offsetExists($offset) 
  48.  
  49.     { 
  50.  
  51.         return isset($this->config[$offset]); 
  52.  
  53.     } 
  54.  
  55.       
  56.  
  57.     public function offsetGet($offset) 
  58.  
  59.     { 
  60.  
  61.         if (emptyempty($this->config[$offset])) { 
  62.  
  63.             $this->config[$offset] = require $this->path.$offset.".php"; 
  64.  
  65.         } 
  66.  
  67.         return $this->config[$offset]; 
  68.  
  69.     } 
  70.  
  71.  
  72.  
  73.     public function offsetSet($offset, $value) 
  74.  
  75.     { 
  76.  
  77.         throw new \Exception('不提供设置配置'); 
  78.  
  79.     } 
  80.  
  81.  
  82.  
  83.     public function offsetUnset($offset) 
  84.  
  85.     { 
  86.  
  87.         throw new \Exception('不提供删除配置'); 
  88.  
  89.     } 
  90.  
  91. } 
  92.  
  93.  
  94.  
  95. $config = Config::instance(); 
  96.  
  97.  
  98.  
  99. //获取app.php 文件的 name 
  100.  
  101. echo $config['app']['name'].PHP_EOL; //app name 
  102.  
  103.  
  104.  
  105. //获取database.php文件mysql的user配置 
  106.  
  107. echo $config['database']['mysql']['user'].PHP_EOL; // root 

Tags: PHP数组式访问 ArrayAccess

分享到: