Many interfaces are provided in php to implement some very specific functions. for example, if you want to use an object as an array, you only need to implement the ArrayAccess interface, to use an object in foreach, you only need to implement the Iterator interface. The following is an example of [php] c... syntaxHighlighter. a provides many interfaces in php to implement some very specific functions. for example, if you want to use an object as an array, you only need to implement the ArrayAccess interface, to use an object in foreach, you only need to implement the Iterator interface, the following is an example of [php] class BtstoreRoot {/*** root node * @ var BtstoreElement */static $ root;} class BtstoreElement implements ArrayAccess, iterator {/*** current directory * @ Var string */private $ dataDir;/*** data currently represented * @ var array */private $ arrData; /*** constructor ** @ param string $ dataDir * @ param array $ arrData */function _ construct ($ dataDir, $ arrData) {$ this-> dataDir = ''; $ this-> arrData = array (); if (! Emptyempty ($ dataDir) & is_dir ($ dataDir) {$ this-> dataDir = $ dataDir;} if (! Emptyempty ($ 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 (! Emptyempty ($ 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 (" un Defined 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: o FfsetGet () */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 BtstoreElem Ent ');}/* (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! Emptyempty ($ data);}/* (non-PHPdoc) * @ see Iterator: rewind () */public function rewind () {reset ($ this-> arrData) ;}}/*** get a BtstoreElement object * @ return BtstoreElement */function btstore_get () {if (emptyempty (BtstoreRoot :: $ root) {BtstoreRoot: $ root = new BtstoreElement (ScriptConf: BTSTORE_ROOT, null);} return BtstoreRoot: $ root ;}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.