PHP provides many interfaces to implement some very specific functions. For example, when you want to use an object as an array, you only need to implement the ArrayAccess interface. When you want to use an object in foreach, you only need to implement Iterator. Interface, here is an example
[php]
class BtstoreRoot
{
/**
* Root node
* @var BtstoreElement
*/
static $root;
}
class BtstoreElement implements ArrayAccess, Iterator
{
/**
* The directory currently represented
* @var string
*/
private $dataDir;
/**
* The 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 ( "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 ! 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;
}
http://www.bkjia.com/PHPjc/477821.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477821.htmlTechArticle provides many interfaces in php to implement some very specific functions. For example, if you want to use an object as When using array, you only need to implement the ArrayAccess interface. When you want to be able to use foreach...