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;}
http://www.bkjia.com/phpjc/371907.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/371907.htmltecharticle在php中提供了许多接口用于实现一些很特定的功能,比如你想把一个对象当作array使用时,只需要实现arrayaccess接口,当你想要foreach中能够...
