mysql resulttype_Mysql中结果集(mysql_result)与Traversable
對于MySQL應該也不是很陌生吧,我常常愛犯的以錯誤就是執行mysli_qurey()后就使用數據,忘記返回的是結果集了。而對于lSELECT,、SHOW, DESCRIBE 、 EXPLAINmysql_query返回的是mysqli_result object,也就是結果集對象;對于其他的mysql_query返回bool值,我在想為啥一個對象可以遍歷呢,查看:
mysqli_result implementsTraversable {/*Properties*/int$current_field;
int$field_count;array $lengths;
int$num_rows;/*Methods*/bool data_seek ( int$offset)mixed fetch_all ([ int $resulttype =MYSQLI_NUM ] )mixed fetch_array ([ int $resulttype =MYSQLI_BOTH ] )arrayfetch_assoc ( void )object fetch_field_direct ( int $fieldnr)objectfetch_field ( void )arrayfetch_fields ( void )object fetch_object ([ string $class_name = "stdClass" [, array $params]] )mixedfetch_row ( void )
bool field_seek ( int$fieldnr)
void free ( void )
}
mysqli_result是實現Traversable接口,手冊是這樣說的:
Iterator support was added, as mysqli_result now implements Traversable.//因為mysqli現在實現了Traversable ,因此迭代被支持
所以查詢返回的結果集對象能被遍歷。
Traverseable介紹
這個接口沒有任何方法,檢測一個類是否可以使用 foreach 進行遍歷的接口。他是Iterator的父接口,所以實現iterator的方法的類可以進行迭代。最底層的,這個接口一般不會去實現它。
任何需要實現Traversable接口的用戶自定義類都必須通過實現從Traversable接口派生的用戶自定義接口來做到這一點, IteratorAggregate 或 Iterator 即是它派生的接口。
if((newB()) instanceof Traversable){foreach () ...}IteratorAggregate介紹
IteratorAggregate接口(是用來將Iterator接口要求實現的5個方法委托給其他類(比如ArrayIterator)來實現)。這讓你可以在類的外部實現迭代功能.并允許重新使用常用的迭代器方法,而不是在編寫的每個可迭代類中重復使用這些方法。
IteratorAggregate extendsTraversable {//實現該方法時,必須返回一個實現了Iterator接口的類的實例
abstract publicTraversable getIterator ( void )
}
其中getIterator 方法返回值必須是能遍歷或實現Iterator接口(must be traversable or implement interface Iterator)。SPL還提供了一些專門用來與IteratorAggregate接口一起使用的內置迭代器。使用這些迭代器意味著只需要實現一個方法并實例化一個類就可以使對象可以迭代訪問了。
class myData implements\IteratorAggregate {public $property1 = "Public property one";public $property2 = "Public property two";public $property3 = array([1,23,4],4,5);public function__construct() {$this->property4 = "last property";
}//實現這個方法
public functiongetIterator() {return new ArrayIterator($this);
}
}$obj = newmyData;foreach($obj as $key => $value) {var_dump($key, $value);echo "\n";
}
來說說相關的接口吧
ArrayAccess
使一個對象可以當數組用,數組式訪問接口(我試了遍歷它不行)。
class ImplementArrayAccess implementsArrayAccess
{private $container = array();public function__construct()
{$this->container = array("one" => 1,
"two" => 2,
"three" => 3,);
}public function offsetSet($offset, $value)
{if (is_null($offset)) {$this->container[] = $value;
}else{$this->container[$offset] = $value;
}
}public function offsetExists($offset)
{return isset($this->container[$offset]);
}public function offsetUnset($offset)
{unset($this->container[$offset]);
}public function offsetGet($offset)
{return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
}$datas = newImplementArrayAccess();$datas['four'] = 4;unset($datas['three']);print_r($datas);
結果
ImplementArrayAccess Object(
[container:ImplementArrayAccess:private] => Array(
[one]=> 1[two]=> 2[four]=> 4)
)
ArrayIterator
這個迭代器允許在遍歷數組和對象時刪除和更新值與鍵
定義的接口:
ArrayIterator implements ArrayAccess , SeekableIterator , Countable ,Serializable {/*方法*/
public void append ( mixed $value)public void asort( void )public __construct ([ mixed $array = array() [, int $flags = 0]] )public int count( void )public mixed current( void )public arraygetArrayCopy ( void )publicvoid getFlags ( void )public mixed key( void )public void ksort( void )public void natcasesort( void )public void natsort( void )public void next( void )public void offsetExists ( string $index)public mixed offsetGet ( string $index)public void offsetSet ( string $index , string $newval)public void offsetUnset ( string $index)public void rewind( void )public void seek ( int $position)public string serialize( void )public void setFlags ( string $flags)public void uasort ( string $cmp_function)public void uksort ( string $cmp_function)public string unserialize ( string $serialized)publicbool valid ( void )
}
例子:
$fruits = array("apple" => "yummy",
"orange" => "ah ya, nice",
"grape" => "wow, I love it!",
"plum" => "nah, not me");$obj = new ArrayIterator( $fruits);var_dump($obj);foreach ($obj as $item){echo $item;
}
ArrayObject
也是讓對象可以當著數組來使用
接口
ArrayObject implements IteratorAggregate , ArrayAccess , Serializable ,Countable {/*常量*/
const integer STD_PROP_LIST = 1;const integer ARRAY_AS_PROPS = 2;/*方法*/
public __construct ([ mixed $input = [] [, int $flags = 0 [, string $iterator_class = "ArrayIterator"]]] )public void append ( mixed $value)public void asort( void )public int count( void )public array exchangeArray ( mixed $input)public arraygetArrayCopy ( void )publicint getFlags ( void )publicArrayIterator getIterator ( void )public stringgetIteratorClass ( void )public void ksort( void )public void natcasesort( void )public void natsort( void )public bool offsetExists ( mixed $index)public mixed offsetGet ( mixed $index)public void offsetSet ( mixed $index , mixed $newval)public void offsetUnset ( mixed $index)public string serialize( void )public void setFlags ( int $flags)public void setIteratorClass ( string $iterator_class)public void uasort ( callable $cmp_function)public void uksort ( callable $cmp_function)public void unserialize ( string $serialized)
}看著似乎ArrayObject與ArrayIterator功能相似,但他們繼承的接口不同,
總結
以上是生活随笔為你收集整理的mysql resulttype_Mysql中结果集(mysql_result)与Traversable的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql创建表关联_MySQL创建高级
- 下一篇: linux cmake编译源码,linu