PHP Generators Explained - Full PHP 8 Tutorial

Поділитися
Вставка
  • Опубліковано 8 лют 2025
  • In this video, we cover what generators are, how they work & their use cases.
    SOME OF THE WAYS YOU CAN SUPPORT THE CHANNEL
    👍 Smash the like button
    🤝 Subscribe to the channel & turn the notifications on
    💬 Post comments, any feedback is greatly appreciated
    ⭐ Become a Patreon: / programwithgio
    THANK YOU!
    COURSE PLAYLIST
    • Learn PHP The Right Wa...
    RESOURCES
    Iterators lesson - • PHP Iterators & Iterab...
    Source Repo - github.com/gge...
    www.php.net/ma...
    www.php.net/ma...

КОМЕНТАРІ • 146

  • @purnimaaggarwal7656
    @purnimaaggarwal7656 Рік тому +7

    I have many years of experience with PHP but was still always confused about Generator until I watched this video. Thank you

    • @ProgramWithGio
      @ProgramWithGio  Рік тому +1

      Super happy to hear that, glad it was helpful 🙌

  • @marrtins
    @marrtins 2 роки тому +23

    I've watched dozens of these PHP series and are they definitely are the best out there! Very concise and comprehensive. Thanks! P.S. 20+ years in programming.

  • @aleksandrkanygin2672
    @aleksandrkanygin2672 3 роки тому +17

    Your course is simply the best php course, thank you for your efforts

  • @jackrourke9237
    @jackrourke9237 3 роки тому +7

    Thank you again for such a beautiful explanation for everything you teach ♥ ♥ ♥ ♥ ♥

  • @Armando-px8hi
    @Armando-px8hi Рік тому

    I do not know how but this video I just saw once and I understood every word that you have said, It is beacuse how you explaing. The greatest coach the PHP has ever seen!!!!

  • @ryanqvincent144
    @ryanqvincent144 Рік тому +1

    Although not explicitly stated, what the 'generator' has to do is 'remember ' the complete current 'state' of the object it is 'part of' so that it can be 'restarted' by the 'next()' function. Obviously, this is the reason for the 'reduced' memory usage.
    It also allows for some 'interesting' coding techniques such as using 'co-routines' in a single PHP execution.
    'Generators' are a very powerful construct indeed. I was surprised to see them in PHP. I agree that most people will use them as shown here.
    Your explanations are very good. thank you. Appreciated.

  • @federicobau8651
    @federicobau8651 3 роки тому +6

    Let me iterate again 🤣👍 Great explanation of generator and very clear, very similar of Python and Javascript.

  • @fifthavenue2105
    @fifthavenue2105 2 роки тому +11

    This video actually does an excellent job explaining the keyword yield! Thank you!!

  • @r1konTheAutomator
    @r1konTheAutomator 10 місяців тому

    I know of and use yield semi regularly, but this is the first time i understood exaclty how it works. Pausing the execution is very counter intuitive in PHP, its almost like goto statements, so its difficult to wrap ones head around. Good stuff my man ❤

  • @abdallakaram5376
    @abdallakaram5376 3 роки тому +1

    🥰 best tutorial and teacher i had seen The great Gio

  • @raerayan
    @raerayan 2 роки тому +5

    As an idea, It would be awesome to hear something about DDD implementation with Laravel or design patterns that you commonly use. But it's already a great job! It's a great course. Thank you very much.

  • @iraqwin9889
    @iraqwin9889 2 роки тому +1

    for($i = 0 ; $i < 1000;$i++){yield "thank you for this lesson"}

  • @Zubbee
    @Zubbee 2 роки тому

    Again, another very valuable lesson that I would have to watch again. Thanks alot Gio.

  • @kalpanaarora7214
    @kalpanaarora7214 2 роки тому +1

    Thank you Gio for the great explaination and the efforts you put

  • @omaryahia
    @omaryahia Рік тому

    amazing, I guess first time I understand the yield statement finally !!🎉 thank you

  • @TemposYS
    @TemposYS Рік тому

    Thank you for so easy explanation, your channel is the best )

  • @alzpk
    @alzpk 3 роки тому +2

    Very helpful and well explained! Thank you!

  • @tolulopemakinde4538
    @tolulopemakinde4538 Рік тому

    This is impressive, I learned a lot in this video

  • @davidbrandt4821
    @davidbrandt4821 2 роки тому

    Great stuff! Like over the top - best I've ever seen! Go on!

  • @IskanderAbbassi
    @IskanderAbbassi 7 місяців тому

    the best one as usual 🙌

  • @judeleon8485
    @judeleon8485 2 роки тому

    Thanks, your tutorials are always well detailed

  • @pavel_espinal
    @pavel_espinal Рік тому

    Succinctly explained. Thanks, man. Great work.

  • @HB9HIL
    @HB9HIL 3 місяці тому

    Amazing and great explained video! thx!

  • @owaisahmed7820
    @owaisahmed7820 3 місяці тому

    Great ❤ Just a kind note: Avoid retrieving all table rows at once even if you are using Generators because it fetches all the rows from the table which is not good if your table hold large amount of data . It's better to implement limit SQL clause to paginate data. He just demonstrate the the uses of generators but it's up to you to properly implement in your application.

  • @ojsojs6004
    @ojsojs6004 3 роки тому +2

    Nice tutorial. I hope you also make one for PHP fiber

  • @emekatimothyiloba699
    @emekatimothyiloba699 Рік тому

    Nice Explanation Gio!!

  • @invgreat5608
    @invgreat5608 2 роки тому +1

    Love you! Perfect videos!

  • @Vitalii-m6r
    @Vitalii-m6r Рік тому

    This is the best PHP course ever. Thanks a million!

  • @processwire-rocks
    @processwire-rocks 2 роки тому +1

    Brilliant explanation, thank you very much!

  • @rushawncampbell7284
    @rushawncampbell7284 2 роки тому

    This was very helpful, I've liked and subscribed !

  • @hossamragab1428
    @hossamragab1428 9 місяців тому

    as usual you are the best
    GIO

  • @manuelcastilloareas
    @manuelcastilloareas 5 місяців тому

    Rermarkeble tutorial! Thanks.

  • @hiteshpatil871
    @hiteshpatil871 2 роки тому

    Amazing explanation . Thanks a lot.

  • @bevedel
    @bevedel 3 роки тому

    Thank you so much Gio. all of your tutorials are awesome and really helpful. waiting for more tutorials from you.
    Is it possible to have more video about advanced laravel?
    Thanks

    • @ProgramWithGio
      @ProgramWithGio  3 роки тому +1

      Thank you. Yes after this PHP series is complete I'll be doing some advanced Laravel

  • @Souljacker7
    @Souljacker7 Рік тому

    Awesome explanation

  • @hansschuijff
    @hansschuijff 2 роки тому +1

    Like all video's I've watched in this series, it has been very enlightening and useful. Excellent content. The speed of it is high, but I can pausing and go back, where needed. I have programmed in php for a while, but not much in OOP and this course offers many subjects that I still wanted to learn and not yet have used or understood. It is the best and most complete course that I've found for php.
    Some sideline reactions that pop up:
    When you state that it isn't production ready because of edge cases and such... and I've heard that disclaimer before ... will the course get students to a point solutions can be used in production? It would be nice to at least know how to get solutions to that point.
    I would like this grows to something full stack including js or react, because even WordPress is expanding in that direction more and more. A second course that adds that would be nice too.
    In the first part of the course I learned about the DIRECTORY_SEPARATOR constant, but I have never seen you used that to build paths. Is that in practice a redundant constant that is only used in the basic lessons?

    • @ProgramWithGio
      @ProgramWithGio  2 роки тому +2

      Thank you for the feedback 🙌. To address your points:
      In some videos I teach how certain things work when you take away the abstraction from frameworks. That's why I state that it's better not to use for production and use a well maintained & battle tested library instead. I only teach how it works so that when you transition into framework you understand it better & is easier to work with.
      We are working on a final project now, not sure if you've seen latest videos. That project we will deploy in production. So yes, I show solutions that can be used in production later on in the series when we get to topics like ORMs, templating engines, micro frameworks & so on.
      This course will not be full stack. I don't teach JS/react. For final project I am providing the front-end and we are building the back-end in videos. I want to end the series soon and since this series is all about PHP, I don't want to mix it with front-end too much.
      About the directory separator, yes in most cases it's not needed, it's there for OS differences but in most cases you deploy apps in Linux based production so using slash shouldn't be a problem. Honestly I think it's fine in Windows too, though I haven't tested it. I know that behind the scenes it does slash conversion anyways so the constant may not be needed at all.

    • @hansschuijff
      @hansschuijff 2 роки тому +1

      @@ProgramWithGio Thank you for answering. I am following the new videos too and the new project you are adding to the course. I'm sure it will be more than enough for me.
      Thank you for sharing the knowledge!

    • @hansschuijff
      @hansschuijff 2 роки тому +1

      @@ProgramWithGio p.s. I work on a windows machine and never had problems with defining paths like you do, so in practice the constant is probably redundant.

  • @giantbush4258
    @giantbush4258 4 місяці тому

    Da best explanation!

  • @remotetechie6910
    @remotetechie6910 3 роки тому

    Great explanation!

  • @koomooboo
    @koomooboo Рік тому

    Thanks for creating these tutorials. They are awesome! When it loops and yields the POD statement in Ticket.php, does it make a database query call for each record?

    • @ProgramWithGio
      @ProgramWithGio  Рік тому

      No it does not, the query is executed before that & yield just generates one value at a time without consuming a lot of memory

    • @necrotikS
      @necrotikS 6 місяців тому

      ​@@ProgramWithGioBut how does it generate it from? Where is the DB data before current() is called? Is it in PHP already? Where is the data from the SELECT * statement?

  • @AMoktar
    @AMoktar Рік тому

    Awesome 👏 thanks bro ❤

  • @golotus
    @golotus 2 роки тому

    Really nice tutorial. Thanks a lot

  • @partoffans5849
    @partoffans5849 7 місяців тому

    thank u for your efforts

  • @benderbg
    @benderbg 5 місяців тому

    Great video. Too bad we didn't have the access to Ticket database table to test this out on our own.

    • @ProgramWithGio
      @ProgramWithGio  5 місяців тому

      @@benderbg thank you. It's easy to recreate, only a few columns

  • @memack101
    @memack101 2 роки тому

    Thanks again.

  • @Zubbee
    @Zubbee 2 роки тому

    Went through again while practicing it. Just beautiful. I have a question that I'm probably asking because I'm considerably new to programing but.. why would one want to or need to rewind an iteration/generator?

    • @ProgramWithGio
      @ProgramWithGio  2 роки тому +1

      You probably would not need to use it since rewind will throw an exception I think if the iteration has begun.

  • @sujanmiya4514
    @sujanmiya4514 2 роки тому

    I love your video

  • @sharifurrobin7720
    @sharifurrobin7720 3 роки тому

    Great job

  • @galihanggorojati3455
    @galihanggorojati3455 3 роки тому

    Thankyou sir 🙏🙏

  • @dawid_dahl
    @dawid_dahl 3 роки тому +1

    ”Let me reiterate”
    Best NPI ever. 😂

  • @dfejescoding
    @dfejescoding 3 роки тому

    Profissional as always.
    Is it possible to use generators for large video files to stream them?

    • @ProgramWithGio
      @ProgramWithGio  3 роки тому +1

      I have not done video processing or uploading so I'm not 100% sure but you could use generators to process large files so perhaps it's possible with video files as well.

  • @DigitaSkills101
    @DigitaSkills101 2 роки тому

    What fonts are you using they are great?
    Speciall the joined signs.

    • @ProgramWithGio
      @ProgramWithGio  2 роки тому

      It's Darcula theme on phpstorm with default settings

  • @artefinal8835
    @artefinal8835 2 роки тому

    was great hear how do you read the text

    • @ProgramWithGio
      @ProgramWithGio  2 роки тому

      Thank you but not sure I understand, what text?

  • @Everythingzof
    @Everythingzof 3 роки тому +1

    Whats the difference between generators and coroutines, because they seem very similar?

    • @ProgramWithGio
      @ProgramWithGio  3 роки тому +1

      Coroutines can be implemented with generators, its just an interruptible function. It can be used to send values back & from generators. (I intentionally omitted that part from this lesson since it can be confusing for beginners)

  • @valentindobrica9584
    @valentindobrica9584 3 роки тому

    Finally bro you posted :D

  • @hex9219
    @hex9219 Рік тому

    you rocks

  • @abdumajidovjafar3460
    @abdumajidovjafar3460 2 роки тому

    Cool

  • @eugenbelousov5027
    @eugenbelousov5027 3 роки тому

    nice

  • @dhirendrasinghnegi7948
    @dhirendrasinghnegi7948 Рік тому

    kindly explain how construct method in model class access app::db without using app class in model class,

    • @ProgramWithGio
      @ProgramWithGio  Рік тому

      We covered that in second section within the Models/PDO lesson. Model is abstract class & in constructor it calls static method db on the App class and assigned to property, then any model extending this base model has access to the db proeprty.

    • @dhirendrasinghnegi7948
      @dhirendrasinghnegi7948 Рік тому +1

      Thanks a lot sir

  • @vladje3747
    @vladje3747 3 роки тому

    Who knows PHP better than creators of course ma man Gio )) Gio tell honestly you know Python as well as PHP? I won't believe you not.

    • @ProgramWithGio
      @ProgramWithGio  3 роки тому

      Haha, no I don't know Python as well as PHP 😁

    • @vladje3747
      @vladje3747 3 роки тому +1

      @@ProgramWithGio you're too modest bro ) I know that you able to build any application better than 80% of programmers , because you really driven in architecture ☝️

    • @ProgramWithGio
      @ProgramWithGio  3 роки тому +2

      @@vladje3747 thank you but I think anyone can build any app if they put enough effort & time into it 🙂

    • @vladje3747
      @vladje3747 3 роки тому +1

      @@ProgramWithGio And motivation , I'm learning python right now and is really easy to me 😂 after 3years of php , python seems to me like nuts hahaha 😂😂😂

    • @ProgramWithGio
      @ProgramWithGio  3 роки тому

      @@vladje3747 nice. Python is great

  • @dhirendrasinghnegi7948
    @dhirendrasinghnegi7948 Рік тому

    my page showing out of memory error even using generators, can you explain why?

    • @dhirendrasinghnegi7948
      @dhirendrasinghnegi7948 Рік тому

      kindly reply

    • @ProgramWithGio
      @ProgramWithGio  Рік тому

      It depends on your configuration, maybe memory limit is low. Also might be an issue with the code, hard to say without looking at the code

    • @dhirendrasinghnegi7948
      @dhirendrasinghnegi7948 Рік тому

      @@ProgramWithGio class GeneratorExampleController
      {
      public function __construct()
      {

      }
      public function index()
      {
      $numbers = $this->lazyRange(1,3000000);
      foreach ($numbers as $key => $number){
      echo $key .':' . $number . '';
      }

      //phpinfo();
      }
      public function lazyRange(int $start , int $end) :Generator
      {
      for ($i = $start; $i

    • @dhirendrasinghnegi7948
      @dhirendrasinghnegi7948 Рік тому

      first it started printing the numbers but after some seconds it shows aw snap, something went wrong while displaying this webpage . error code: out of memory in google chrome

    • @ProgramWithGio
      @ProgramWithGio  Рік тому

      That's just the browser is not able to display/render so much content. You are trying to print 3 million lines of HTML essentially. If you were to display 3 million rows of data on a page it would crash also. You would typically use pagination for that.

  • @truthteachers
    @truthteachers Рік тому

    Bro, I got stumped at timestamp 18:27. Its about the database and data for the tickets table. I could not find them in the repo. Please advise.

    • @ProgramWithGio
      @ProgramWithGio  Рік тому

      It's just dummy data and table I created to demonstrate generators, it doesn't matter what data it will be, you can also create some table and populate it with a lot of records. Just name it tickets.

    • @truthteachers
      @truthteachers Рік тому +1

      @@ProgramWithGio I was lazy. Created and tested. Works great. Thank you so much.

  • @programming_tips_bd
    @programming_tips_bd 3 роки тому +1

    Thank you again for such a beautiful explanation for everything you teach ♥ ♥ ♥ ♥ ♥