What is a php iterator
This article introduces the knowledge of "what is php iterator". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Php iterator is a PHP design pattern, which refers to the interface of one's own external iterator or class that can be iterated internally; the iterator pattern can traverse the internal elements of an aggregate object without knowing the internal implementation.
This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer
What does php iterator mean?
PHP iterator mode
Iterator: the class inherits PHP's Iterator interface and operates in batches.
1. The iterator pattern traverses the internal elements of an aggregate object without knowing the internal implementation.
two。 Compared to the traditional programming mode, the iterator pattern can hide the actions required to traverse elements.
Interface Iterator
Current () returns the current element
Key () returns the key of the current element
Next () moves forward to the next element
Rewind () returns to the first element of the iterator
Class AllUser implements\ Iterator {protected $index = 0; protected $data = []; public function _ _ construct () {$link = mysqli_connect ('192.168.0.91,' root', '123,' xxx'); $rec = mysqli_query ($link, 'select id from doc_admin'); $this- > data = mysqli_fetch_all ($rec, MYSQLI_ASSOC) } / / 1 reset iterator public function rewind () {$this- > index = 0;} xxx / / 2 verify whether the iterator has data public function valid () {return $this- > index
< count($this->Data);} / / 3 get current content public function current () {$id = $this- > data [$this- > index]; return User::find ($id);} / / 4 move key to the next public function next () {return $this- > index++;} / / 5 iterator location key public function key () {return $this- > index }} / / implement iterative traversal of user table $users = new AllUser (); / / real-time modification of foreach ($users as $user) {$user- > add_time = time (); $user- > save ();} "what is an php iterator" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!