php重載數組操作符

來源:互聯網
上載者:User

在php中提供了許多介面用於實現一些很特定的功能,比如你想把一個對象當作array使用時,只需要實現ArrayAccess介面,當你想要foreach中能夠使用一個對象時,只需要實現Iterator介面,下面給一個例子出來

class BtstoreRoot{/** * 根結點 * @var BtstoreElement */static $root;}class BtstoreElement implements ArrayAccess, Iterator{/** * 當前所代表的目錄 * @var string */private $dataDir;/** * 當前所代表的資料 * @var array */private $arrData;/** * 建構函式 * @param string $dataDir * @param array $arrData */function __construct($dataDir, $arrData){$this->dataDir = '';$this->arrData = array ();if (! empty ( $dataDir ) && is_dir ( $dataDir )){$this->dataDir = $dataDir;}if (! empty ( $arrData )){$this->arrData = $arrData;}}function __get($key){if (isset ( $this->arrData [$key] )){$data = $this->arrData [$key];if (is_array ( $data ) && ! is_object ( $data )){$data = new BtstoreElement ( '', $data );}return $data;}if (! empty ( $this->dataDir )){$path = $this->dataDir . '/' . $key;if (is_dir ( $path )){$data = new BtstoreElement ( $path, null );$this->arrData [$key] = $data;return $data;}if (is_file ( $path )){$content = file_get_contents ( $path );$arrData = unserialize ( $content );$data = new BtstoreElement ( '', $arrData );$this->arrData [$key] = $data;return $data;}}trigger_error ( "undefined index:$key" );}function __isset($key){if (isset ( $this->arrData [$key] )){return true;}if (file_exists ( $this->dataDir . '/' . $key )){return true;}return false;}function toArray(){return $this->arrData;}/* (non-PHPdoc) * @see ArrayAccess::offsetExists() */public function offsetExists($offset){return $this->__isset ( $offset );}/* (non-PHPdoc) * @see ArrayAccess::offsetGet() */public function offsetGet($offset){return $this->__get ( $offset );}/* (non-PHPdoc) * @see ArrayAccess::offsetSet() */public function offsetSet($offset, $value){trigger_error ( 'offsetSet not implemented by BtstoreElement' );}/* (non-PHPdoc) * @see ArrayAccess::offsetUnset() */public function offsetUnset($offset){trigger_error ( 'offsetUnset not implemented by BtstoreElement' );}/* (non-PHPdoc) * @see Iterator::current() */public function current(){return current ( $this->arrData );}/* (non-PHPdoc) * @see Iterator::next() */public function next(){return next ( $this->arrData );}/* (non-PHPdoc) * @see Iterator::key() */public function key(){return key ( $this->arrData );}/* (non-PHPdoc) * @see Iterator::valid() */public function valid(){$data = current ( $this->arrData );return ! empty ( $data );}/* (non-PHPdoc) * @see Iterator::rewind() */public function rewind(){reset ( $this->arrData );}}/** * 擷取一個BtstoreElement對象 * @return BtstoreElement */function btstore_get(){if (empty ( BtstoreRoot::$root )){BtstoreRoot::$root = new BtstoreElement ( ScriptConf::BTSTORE_ROOT, null );}return BtstoreRoot::$root;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.