Skip to main content

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 key that will be used to iterate with in class.
  • next() – This function is same as for loop increment part.
  • rewind() – This function is same as for loop initialization part.
  • valid() – This function is same as for loop condition checking.

आइये अब iteration का एक उदाहरण देखते है।

<?php

// Implementing iterator interface
class myClass implements Iterator
{
    public function myFunction()
    {
        echo "<b>Welcome</b><br>";
    }
    
    
    // For loop initialization
    function rewind()
    {
        $this->i = 0;
    }
    
    
    // For loop body
    function current()
    {
        echo $i = "<b>Hi,</b>";
        $this->myFunction();
    }
    
    
    // Key to iterate i
    function key()
    {
        return $this->i;
    }
    
    
    // Increment part of for loop
    function next()
    {
        $this->i++;
    }
    
    
    function valid()
    {
        return $this->i <= 5;
    }
    
    
}


$obj = new myClass();


// For each loop iterating a class
foreach ($obj as $value) {
    echo $value;
}

?>

 

ये एक बहुत ही simple उदाहरण है यदि आप for each लूप का उपयोग जानते है तो आसानी से इसे समझ सकते है। यह उदाहरण निचे दिया गया output generate करता है।




Next class part 👉 12

 

Comments

Popular posts from this blog

PHP data types (PHP class part 3 in Hindi )

  PHP data types Introduction to PHP Data Types आप किस तरह की information किसी variable में store करना चाहते है ये आप PHP data types के द्वारा define करते है। जैसे की आप कोई संख्या store करना चाहते है। या फिर character store करना चाहते ये आप data types के माध्यम से compiler को दर्शाते है। PHP में 8 तरह के data types होते है। जिनमे 4 scalar types होते है, 2 compound types होते है और 2 special types होते है। Scalar types Integer Float  Boolean String Compound types Array Object Special types Resource NULL Next class part 4  

PHP tags (PHP class part 2 in Hindi )

  PHP tags  Introduction to PHP Tags जब कोई PHP file parse होती है तो parser opening और closing tags को search करता है। Basically parser interpreter का part होता है और PHP code को अलग अलग हिस्सों में तोड़ता है ताकि पता लग सके की कौनसा हिस्सा tag है और कौनसा actual data है। Parser वही कोड parse करता है जो opening और closing tags के बीच में हो बाकि सारा कोड parser ignore कर देता है। ऐसा हो सकता है की आपको अपने document में कई अलग अलग जगह पर PHP code यूज़ करना पड़े। यदि आप ऐसा बिना PHP tags को define किये करते है तो parser उस PHP code को execute नहीं करेगा। इसलिए आप जँहा भी PHP code लिखेंगे वह PHP के opening और closing tags के बीच में ही होगा। Types of PHP Tags PHP 4 तरह के tags provide करती है। हालाँकि PHP के latest version में इनमें से कुछ tags को disallow कर दिया गया है। निचे इन tags के बारे में बताया जा रहा है।   Next class part 3   

Learn PHP in Hindi ( PHP class part 👉 1) join now free

  Learn PHP in Hindi Introduction to PHP Hypertext Preprocessor PHP के बारे में detail से जानने से पूर्व आइये जानते है की PHP की आवश्यकता क्यों है? HTML एक बहुत ही आसान और उपयोगी language है। HTML के द्वारा आप web pages design करते है। HTML के साथ CSS के उपयोग से आप web pages को और भी beautiful बनाते है। लेकिन HTML सिर्फ web pages के structure और designing तक ही सिमित है। ऐसे कई tasks है जो HTML द्वारा नहीं perform किये जा सकते है। जैसे की – HTML के द्वारा आप information server पर store नहीं कर सकते है और server से कोई information access भी नहीं कर सकते है। इसके अलावा HTML के द्वारा आप databases में data store नहीं कर सकते है और database से data access भी नहीं कर सकते है।  और HTML के द्वारा webpages में logical/mathematical tasks भी perform नहीं किये जा सकते है।  उदाहरण के लिए मान लीजिये आपने एक HTML form create किया है। इस form में user द्वारा data input किया जाता है। User द्वारा input किए गए data को आप किसी server या database में store करना चाहते है। इसी situa...