PHP Generators Explained - Full PHP 8 Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024
  • 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...

ความคิดเห็น • 138

  • @marrtins
    @marrtins ปีที่แล้ว +22

    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.

    • @ProgramWithGio
      @ProgramWithGio  ปีที่แล้ว +1

      Glad you like them, thank you 💙

  • @purnimaaggarwal7656
    @purnimaaggarwal7656 ปีที่แล้ว +6

    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 🙌

  • @aleksandrkanygin2672
    @aleksandrkanygin2672 2 ปีที่แล้ว +16

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

  • @jackrourke9237
    @jackrourke9237 2 ปีที่แล้ว +7

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

  • @fifthavenue2105
    @fifthavenue2105 ปีที่แล้ว +11

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

  • @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!!!!

  • @pavel_espinal
    @pavel_espinal 11 หลายเดือนก่อน

    Succinctly explained. Thanks, man. Great work.

    • @ProgramWithGio
      @ProgramWithGio  10 หลายเดือนก่อน +1

      Glad it was helpful

  • @federicobau8651
    @federicobau8651 2 ปีที่แล้ว +6

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

  • @raerayan
    @raerayan ปีที่แล้ว +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.

    • @ProgramWithGio
      @ProgramWithGio  ปีที่แล้ว +1

      Thank you for the suggestion 🙌

  • @omaryahia
    @omaryahia 9 หลายเดือนก่อน

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

    • @ProgramWithGio
      @ProgramWithGio  9 หลายเดือนก่อน +1

      Awesome, glad to hear

  • @invgreat5608
    @invgreat5608 ปีที่แล้ว +1

    Love you! Perfect videos!

  • @OutRager
    @OutRager 2 ปีที่แล้ว +2

    Very helpful and well explained! Thank you!

  • @abdallakaram5376
    @abdallakaram5376 2 ปีที่แล้ว +1

    🥰 best tutorial and teacher i had seen The great Gio

  • @r1konTheAutomator
    @r1konTheAutomator 6 หลายเดือนก่อน

    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 ❤

    • @ProgramWithGio
      @ProgramWithGio  5 หลายเดือนก่อน

      Awesome, happy to hear 💙

  • @emekatimothyiloba699
    @emekatimothyiloba699 ปีที่แล้ว

    Nice Explanation Gio!!

  • @kalpanaarora7214
    @kalpanaarora7214 2 ปีที่แล้ว +1

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

  • @TemposYS
    @TemposYS 9 หลายเดือนก่อน

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

  • @ryanqvincent144
    @ryanqvincent144 11 หลายเดือนก่อน

    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.

    • @ProgramWithGio
      @ProgramWithGio  11 หลายเดือนก่อน +1

      Good points, thank you 💙

    • @r1konTheAutomator
      @r1konTheAutomator 6 หลายเดือนก่อน

      Boy you really like those quotes 😅

  • @IskanderAbbassi
    @IskanderAbbassi 2 หลายเดือนก่อน

    the best one as usual 🙌

  • @Zubbee
    @Zubbee 2 ปีที่แล้ว

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

  • @iraqwin9889
    @iraqwin9889 ปีที่แล้ว +1

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

  • @tolulopemakinde4538
    @tolulopemakinde4538 ปีที่แล้ว

    This is impressive, I learned a lot in this video

  • @processwire-rocks
    @processwire-rocks 2 ปีที่แล้ว +1

    Brilliant explanation, thank you very much!

    • @ProgramWithGio
      @ProgramWithGio  2 ปีที่แล้ว

      You're very welcome, thank you 💙

  • @ojsojs6004
    @ojsojs6004 2 ปีที่แล้ว +2

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

  • @manuelcastilloareas
    @manuelcastilloareas 27 วันที่ผ่านมา

    Rermarkeble tutorial! Thanks.

  • @hossamragab1428
    @hossamragab1428 4 หลายเดือนก่อน

    as usual you are the best
    GIO

  • @judeleon8485
    @judeleon8485 ปีที่แล้ว

    Thanks, your tutorials are always well detailed

    • @ProgramWithGio
      @ProgramWithGio  ปีที่แล้ว

      You're welcome. Glad you like it 🙌

  • @davidbrandt4821
    @davidbrandt4821 2 ปีที่แล้ว

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

  • @Souljacker7
    @Souljacker7 ปีที่แล้ว

    Awesome explanation

  • @rushawncampbell7284
    @rushawncampbell7284 2 ปีที่แล้ว

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

    • @ProgramWithGio
      @ProgramWithGio  2 ปีที่แล้ว

      Glad to hear that, thank you 🙏

  • @partoffans5849
    @partoffans5849 2 หลายเดือนก่อน

    thank u for your efforts

  • @AMoktar
    @AMoktar 11 หลายเดือนก่อน

    Awesome 👏 thanks bro ❤

  • @hiteshpatil871
    @hiteshpatil871 ปีที่แล้ว

    Amazing explanation . Thanks a lot.

  • @remotetechie6910
    @remotetechie6910 2 ปีที่แล้ว

    Great explanation!

  • @memack101
    @memack101 2 ปีที่แล้ว

    Thanks again.

  • @bevedel
    @bevedel 2 ปีที่แล้ว

    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  2 ปีที่แล้ว +1

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

  • @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 หลายเดือนก่อน

      ​@@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?

  • @benderbg
    @benderbg 19 วันที่ผ่านมา

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

    • @ProgramWithGio
      @ProgramWithGio  19 วันที่ผ่านมา

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

  • @hansschuijff
    @hansschuijff ปีที่แล้ว +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

      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 ปีที่แล้ว +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 ปีที่แล้ว +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.

  • @Vitalii-m6r
    @Vitalii-m6r ปีที่แล้ว

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

  • @golotus
    @golotus 2 ปีที่แล้ว

    Really nice tutorial. Thanks a lot

  • @sujanmiya4514
    @sujanmiya4514 ปีที่แล้ว

    I love your video

  • @Everythingzof
    @Everythingzof 2 ปีที่แล้ว +1

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

    • @ProgramWithGio
      @ProgramWithGio  2 ปีที่แล้ว +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)

  • @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.

  • @sharifurrobin7720
    @sharifurrobin7720 2 ปีที่แล้ว

    Great job

  • @dawid_dahl
    @dawid_dahl 2 ปีที่แล้ว +1

    ”Let me reiterate”
    Best NPI ever. 😂

  • @galihanggorojati3455
    @galihanggorojati3455 2 ปีที่แล้ว

    Thankyou sir 🙏🙏

  • @hex9219
    @hex9219 ปีที่แล้ว

    you rocks

  • @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?

  • @dfejescoding
    @dfejescoding 2 ปีที่แล้ว

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

    • @ProgramWithGio
      @ProgramWithGio  2 ปีที่แล้ว +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.

  • @abdumajidovjafar3460
    @abdumajidovjafar3460 ปีที่แล้ว

    Cool

  • @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

  • @eugenbelousov5027
    @eugenbelousov5027 2 ปีที่แล้ว

    nice

  • @valentindobrica9584
    @valentindobrica9584 2 ปีที่แล้ว

    Finally bro you posted :D

  • @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.

  • @vladje3747
    @vladje3747 2 ปีที่แล้ว

    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  2 ปีที่แล้ว

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

    • @vladje3747
      @vladje3747 2 ปีที่แล้ว +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  2 ปีที่แล้ว +2

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

    • @vladje3747
      @vladje3747 2 ปีที่แล้ว +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  2 ปีที่แล้ว

      @@vladje3747 nice. Python is great

  • @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 2 ปีที่แล้ว +1

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