Skip to main content

Posts

Showing posts with the label PHP iterators and generators

PHP iterators and generators , Introduction to PHP Iterators ( PHP class part 11

  PHP iterators and generators  Introduction to PHP Iterators  PHP में iterators का प्रयोग objects को iterate करने के लिए किया जाता है। इसे आप ऐसे समझ सकते है। जैसा की आपको पता है PHP में arrays objects है। आप for each लूप का प्रयोग करते हुए array के सभी elements को print करवाते है। Iterate a Class उसी प्रकार आप for each loop में किसी class का object भी पास कर सकते है और उस class को iterate कर सकते है। <?php $arr = array(1,2,3); // Iterating array foreach($arr as $value) { echo $value; } ?> Implement Iterator Interface लेकिन class को for each लूप के द्वारा iterate करने के लिए आपको अपनी class द्वारा Iterator interface को implement करना होगा। और Iterator interface के 5 abstract methods की definition अपनी class में provide करनी होगी। ये methods for loop की तरह ही काम करते है। ये methods निचे दिए जा रहे है – current() – Used to print current value or perform current task. It is same as for loop body. key() – This function represent the...