国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP數(shù)組式訪問-ArrayAccess示例解析

2020-03-22 19:53:11
字體:
供稿:網(wǎng)友
本文章主要講述了PHP中的數(shù)組式訪問,具有一定參考價值,感興趣的朋友可以了解一下,希望能幫助到你。

以前對ArrayAccess不是很熟悉,現(xiàn)在整理下下有關(guān)ArrayAccess相關(guān)的知識,ArrayAccess接口就是提供像訪問數(shù)組一樣訪問對象的能力的接口。

接口內(nèi)容如下:

ArrayAccess { //檢查一個偏移位置是否存在  abstract html' target='_blank'>public boolean offsetExists ( mixed $offset );  //獲取一個偏移位置的值  abstract public mixed offsetGet ( mixed $offset );  //設(shè)置一個偏移位置的值  abstract public void offsetSet ( mixed $offset , mixed $value );  //復(fù)位一個偏移位置的值  abstract public void offsetUnset ( mixed $offset ); }

項目中使用,獲取網(wǎng)站配置:

 ?phpnamespace lib;use mpf/core/Di;class config implements /ArrayAccess{//定義存儲數(shù)據(jù)的數(shù)組 protected $configs; public function __construct($configs){ $this- configs = $configs; $configs = /lib/model/Home::getWebConfig(); foreach( $configs as $config ){ if( !isset($this- configs[$config[ sc_key ]]) ){ $this- configs[$config[ sc_key ]] = $config[ sc_content  public function get($key){ if( isset($this- configs[$key]) ){ return $this- configs[$key]; }elseif( $key == caipiao ){ $this- configs[ caipiao ] = /lib/model/Home::getLcs();  return $this- configs[$key]; }elseif( $key == user_money ){ if( isset($_SESSION[ uid ]) ){ if( $_SESSION[ utype ] == 5 ){ $sql = select money from inner_user where uid=?  }else{ $sql = select money from user where uid=?  $this- configs[ user_money ] = /mpf/core/Di::$Di- db- prepare_query($sql,[getUid()])- fetch(/PDO::FETCH_COLUMN); return $this- configs[ user_money  public function offsetExists($index){ return isset($this- configs[$index]); public function offsetGet($index){ return $this- configs[$index]; public function offsetSet($index,$val){ $this- configs[$index] = $val; public function offsetUnset($index){ unset($this- configs[$index]);}

這樣可以使用config對象來直接訪問配置信息內(nèi)容。

配置程序:

我們可以通過ArrayAccess利用配置文件來控制程序。

1. 在項目更目錄下創(chuàng)建一個config目錄
2. 在config目錄下創(chuàng)建相應(yīng)的配置文件,比如app.php 和 database.php。文件程序如下

app.php

 ?phpreturn [  name = app name ,  version = v1.0.0 ];

database.php

 ?phpreturn [ mysql = [ host = localhost , user = root , password = 12345678 ];

3. Config.php實現(xiàn)ArrayAccess

 ?phpnamespace Config;class Config implements /ArrayAccess private $config = []; private static $instance; private $path; private function __construct() $this- path = __DIR__. /config/  public static function instance() 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 (empty($this- config[$offset])) { $this- config[$offset] = require $this- path.$offset. .php  return $this- config[$offset]; public function offsetSet($offset, $value) throw new /Exception( 不提供設(shè)置配置  public function offsetUnset($offset) throw new /Exception( 不提供刪除配置 $config = Config::instance();//獲取app.php 文件的 nameecho $config[ app ][ name ].PHP_EOL; //app name//獲取database.php文件mysql的user配置echo $config[ database ][ mysql ][ user ].PHP_EOL; // root

相關(guān)教程:PHP視頻教程

以上就是PHP數(shù)組式訪問-ArrayAccess示例解析的詳細內(nèi)容,PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 西宁市| 望谟县| 三门峡市| 库车县| 菏泽市| 新丰县| 涿州市| 襄城县| 望谟县| 新巴尔虎右旗| 烟台市| 将乐县| 吐鲁番市| 四子王旗| 郯城县| 龙州县| 嘉祥县| 肇东市| 云安县| 高尔夫| 合作市| 凯里市| 永年县| 余庆县| 会宁县| 吉安市| 库车县| 丹阳市| 华坪县| 宁都县| 伊吾县| 宿州市| 桐乡市| 武川县| 繁峙县| 海门市| 汶川县| 博湖县| 赞皇县| 深州市| 林西县|