The
Provides a number of interfaces in PHP to implement some very specific features, such as when you want to use an object as an array, just implement the Arrayaccess interface, and when you want to be able to use an object in a foreach, just implement the iterator interface, The following gives an example of the
Class Btstoreroot {/** * root node * @var btstoreelement/static $root;}
Class Btstoreelement implements Arrayaccess, iterator {/** * currently represents the directory * @var String */private $dataDir;
/** * currently represents the data * @var Array * * Private $arrData; /** * constructor * @param string $dataDir * @param array $arrData/function __construct ($dataDir, $arrData) {$th
Is->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");
The function __isset ($key) {if (Isset ($this->arrdata [$key])) {return true; The 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-&G
T;__isset ($offset); }/* (non-phpdoc) * @see arrayaccess::offsetget () */Public Function Offsetget ($offset) {return $this->__ge
T ($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); /** * Gets a Btstoreelement object * @return btstoreelement/function Btstore_get () {If Empty (btstoreroot:: $root ) {btstoreroot:: $root = new Btstoreelement (scriptconf::btstore_root, NULL);
Return Btstoreroot:: $root; }