How foreach of PHP works with MongoDB Cursor? -
there functions use mongodb driver iterate cursor can use "hasnext()" , "getnext()". can use simple php's foreach iterate cursor , don't have call above methods.
my question if foreach simple php function, how work mongodb ? how fetches records cursor ? internally use "cursor.foreach()" of mongodb or internally runs "hasnext()" , "getnext()" ?
any appreciated.
the mongocursor class implements iterator
interface. so, foreach
ing cursor, same thing calling $cursor->next()
, checking $cursor->valid()
, getting value $cursor->current()
(repeat until valid
false
). $key => $value
format gets value of $cursor->key()
.
it internal runs not running "hasnext()", "getnext()", etc. runs standard methods abstractly described in iterator
interface.
sorry captiousness, foreach
not "simple php function"; it's language construct. i've found explanations the difference here on stack overflow.
Comments
Post a Comment