Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Iterators

 Iterators

Iterators a super powerful tool that is part of the SPL.

Companion Code: https://github.com/jtyost2/LasVegas-PHP-Iterators

Justin Yost

October 05, 2014
Tweet

More Decks by Justin Yost

Other Decks in Programming

Transcript

  1. So#what#does#a#Standard#foreach#look#like? $someArrayOfStuff = array(); foreach($someArrayOfStuff as $key => $value) {

    $someArrayOfStuff[$key] = mainpulateValue($value); } foreach($someArrayOfStuff as $key => $value) { displayStuff($value); } Pre$y&standard&pa$ern&in&PHP
  2. Imagine(doing(some(manipula/on(mul/ple(/mes? $someDataInput = array(); foreach($someDataInput as $key => $data) {

    $someDataInput[$key] = manipulateTheData($data); } foreach($someDataInput as $key => $data) { $someDataInput[$key] = caclulateStatisticsOfManipulateData($data); } foreach($someDataInput as $key => $data) { displayData($data); }
  3. Iterators(are(the(solu,on Iterators(follow(a(basic(pa0ern: Think&the&normal&foreach&loop& foreach($obj as $key => $value) $iterator->rewind(); while

    ($iterator->valid()) { $iterator->current(); //$value $iterator->key(); //$key $iterator->next(); } Keep$this$pa*ern$in$mind.