【php开源代码栏目提醒】:网学会员在php开源代码频道为大家收集整理了ResultIterator.class.php提供大家参考,希望对大家有所帮助!
<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$
/**
+------------------------------------------------------------------------------
* ResultIterator绫?鐢ㄤ簬瀹炵幇鏁版嵁搴撶殑寤惰繜鍔犺浇
* TODO 瀹炵幇SQL鐨勫姩鎬佺粍瑁?
+------------------------------------------------------------------------------
* @category Think
* @package Think
* @subpackage Db
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/
class ResultIterator extends Base implements IteratorAggregate
{
// 鎵ц鏌ヨ鐨凷QL
private $sql = null;
// 鏌ヨ鐨勫璞″皝瑁?
private $map = null;
// 鏁版嵁搴撴搷浣滃璞?
private $db = null;
// 杩斿洖鐨勬煡璇㈡暟鎹殑鏁扮洰
private $size = null;
// 杩斿洖鐨勬煡璇㈡暟鎹?
private $data = null;
/**
+----------------------------------------------------------
* 鏋舵瀯鍑芥暟
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $array 鍒濆鍖栨暟缁勫厓绱?
+----------------------------------------------------------
*/
public function __construct($sql='')
{
$this->sql = $sql;
}
/**
+----------------------------------------------------------
* 鑾峰彇Iterator鍥犲瓙
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return Iterate
+----------------------------------------------------------
*/
public function getIterator()
{
$result = $this->getData();
return $result;
}
/**
+----------------------------------------------------------
* 瀹為檯鑾峰彇鏌ヨ缁撴灉
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return ArrayObject
+----------------------------------------------------------
*/
public function getData() {
if(empty($this->data)) {
$this->db = Db::getInstance();
$this->data = $this->db->query($this->sql);
$this->size = count($this->data);
}
return $this->data;
}
/**
+----------------------------------------------------------
* 鑾峰彇鏌ヨ缁撴灉鏁扮洰
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
*/
public function size() {
if(empty($this->size)) {
$this->getData();
}
return $this->size;
}
};
?>