PHP overloaded array operator

Source: Internet
Author: User

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; }

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.