php重载数组操作符_PHP教程

WBOY
发布: 2016-07-20 11:17:57
原创
1667 人浏览过

在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;
}
登录后复制

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/371907.htmlTechArticle在php中提供了许多接口用于实现一些很特定的功能,比如你想把一个对象当作array使用时,只需要实现ArrayAccess接口,当你想要foreach中能够...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!