mysql resulttype_Mysql中结果集(mysql_result)与Traversable
對(duì)于MySQL應(yīng)該也不是很陌生吧,我常常愛(ài)犯的以錯(cuò)誤就是執(zhí)行mysli_qurey()后就使用數(shù)據(jù),忘記返回的是結(jié)果集了。而對(duì)于lSELECT,、SHOW, DESCRIBE 、 EXPLAINmysql_query返回的是mysqli_result object,也就是結(jié)果集對(duì)象;對(duì)于其他的mysql_query返回bool值,我在想為啥一個(gè)對(duì)象可以遍歷呢,查看:
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是實(shí)現(xiàn)Traversable接口,手冊(cè)是這樣說(shuō)的:
Iterator support was added, as mysqli_result now implements Traversable.//因?yàn)閙ysqli現(xiàn)在實(shí)現(xiàn)了Traversable ,因此迭代被支持
所以查詢返回的結(jié)果集對(duì)象能被遍歷。
Traverseable介紹
這個(gè)接口沒(méi)有任何方法,檢測(cè)一個(gè)類是否可以使用 foreach 進(jìn)行遍歷的接口。他是Iterator的父接口,所以實(shí)現(xiàn)iterator的方法的類可以進(jìn)行迭代。最底層的,這個(gè)接口一般不會(huì)去實(shí)現(xiàn)它。
任何需要實(shí)現(xiàn)Traversable接口的用戶自定義類都必須通過(guò)實(shí)現(xiàn)從Traversable接口派生的用戶自定義接口來(lái)做到這一點(diǎn), IteratorAggregate 或 Iterator 即是它派生的接口。
if((newB()) instanceof Traversable){foreach () ...}IteratorAggregate介紹
IteratorAggregate接口(是用來(lái)將Iterator接口要求實(shí)現(xiàn)的5個(gè)方法委托給其他類(比如ArrayIterator)來(lái)實(shí)現(xiàn))。這讓你可以在類的外部實(shí)現(xiàn)迭代功能.并允許重新使用常用的迭代器方法,而不是在編寫的每個(gè)可迭代類中重復(fù)使用這些方法。
IteratorAggregate extendsTraversable {//實(shí)現(xiàn)該方法時(shí),必須返回一個(gè)實(shí)現(xiàn)了Iterator接口的類的實(shí)例
abstract publicTraversable getIterator ( void )
}
其中g(shù)etIterator 方法返回值必須是能遍歷或?qū)崿F(xiàn)Iterator接口(must be traversable or implement interface Iterator)。SPL還提供了一些專門用來(lái)與IteratorAggregate接口一起使用的內(nèi)置迭代器。使用這些迭代器意味著只需要實(shí)現(xiàn)一個(gè)方法并實(shí)例化一個(gè)類就可以使對(duì)象可以迭代訪問(wèn)了。
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";
}//實(shí)現(xiàn)這個(gè)方法
public functiongetIterator() {return new ArrayIterator($this);
}
}$obj = newmyData;foreach($obj as $key => $value) {var_dump($key, $value);echo "\n";
}
來(lái)說(shuō)說(shuō)相關(guān)的接口吧
ArrayAccess
使一個(gè)對(duì)象可以當(dāng)數(shù)組用,數(shù)組式訪問(wèn)接口(我試了遍歷它不行)。
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);
結(jié)果
ImplementArrayAccess Object(
[container:ImplementArrayAccess:private] => Array(
[one]=> 1[two]=> 2[four]=> 4)
)
ArrayIterator
這個(gè)迭代器允許在遍歷數(shù)組和對(duì)象時(shí)刪除和更新值與鍵
定義的接口:
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
也是讓對(duì)象可以當(dāng)著數(shù)組來(lái)使用
接口
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功能相似,但他們繼承的接口不同,
總結(jié)
以上是生活随笔為你收集整理的mysql resulttype_Mysql中结果集(mysql_result)与Traversable的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql创建表关联_MySQL创建高级
- 下一篇: mysql5.7.12 64位解压版_m