PHP Iterators & Iterable Type - Iterate Over Objects - Full PHP 8 Tutorial

Поділитися
Вставка
  • Опубліковано 3 лют 2025

КОМЕНТАРІ • 86

  • @ousssika5359
    @ousssika5359 Рік тому +4

    i have been using youtube for 15 years to study , hands down your the best teacher , tha a man can hope for

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

    Hands down, this is the best explanation of Iterators I've even seen. Thanks!

  • @lynic-0091
    @lynic-0091 Рік тому +2

    Jaw drop, I had no friggin clue something like this was possible. This is the best PHP course I've ever seen.

  • @Nooneandeveryone0101
    @Nooneandeveryone0101 2 роки тому +10

    Every time I see the number of subscribers increased I feel really happy, cause I hope every body watches this series in which you're making programming more exciting and more fun! thank you

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

    I'm really enjoying this series and learning more
    Respect from Pakistan.

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

    This the best class about iterators with examples. Keep it up will done.

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

    Extremely simple explanation

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

    Thank you so much for sharing this content of excellent quality.

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

    i love you bro thank you so much for all those videos clear and simple example that make it easy.

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

    Great lesson, I've bagged another one (need to practice though). Thanks alot Gio.

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

    Thank you for this lesson!

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

    instead of implementing iterator, why not just use nested foreach loop?
    or you just did it to show the implementation of iterator?

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

      It depends on the data structure of what you are iterating over. If its just simple arrays then yea sure you should just use foreach loop on the array, if its a complex data structure or you need more control over how its iterated over then you implement iterator.

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

    Thank you Gio

  • @KochADV
    @KochADV 2 місяці тому

    great lesson. thanks

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

    Thank you GIO:)

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

    amazing 🌟

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

    Thank you! "Invoice" inside "InvoiceList" iterable must have public properties, but what if I don't want to expose the internal state of that object. What are the options for this?

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

      In PHP 8.1 you can make properties read-only, that way you can have public property accessible but can't be changed. You can also just make them private/protected and create appropriate getter/setters.

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

    Great lesson, thank you!

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

    Thank you for tor sharing this, great video!

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

    Hi, may i ask that next method 4:35 must return void, why are you returning next($this->invoices) ? Its throwing errror in my IDE

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

      Hey, what error are you getting? The next method does not need to return anything, it can be void, though in some cases it might be useful to return something which is what I'm showing. You can have it as void & just remove the return part, make sure to still have the next($this->invoice)

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

      @@ProgramWithGio Error was incompatible interface, so i got rid of return stmt and it worked.

  • @ינוןאלבז-כ1ז
    @ינוןאלבז-כ1ז 2 роки тому

    I dont know any of that topic. Thanks!!!!!!

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

    I've been watching this video as guide to iterate over an object (deep nested, tree data) using RecursiveIteratorIterator & RecursiveArrayIterator but couldn't make it work.
    Any enlightenment on those recursive classes?

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

      What is the issue that you are having, can you share more details?

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

    Thanks!

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

    Hello Sir, I am having a problem when I use the iterator interface, I am getting the result but before it there is a message telling me that:
    Deprecated: Return type of app\InvoiceCollection::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

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

      Hello, add mixed as return type to your key method.

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

      @@ProgramWithGio thank you very much it works

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

    Great lesson, it would be greater if you add an iterator example of linked list? Thank you :)

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

      Thank you & thanks for the suggestion 💙

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

    Thank you.

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

    I didn't quite get why variable $key in the InvoiceCollection class got initialized with 0 whereas you haven't explicitly set it to be. Is this some feature of php8 or am I missing something? Because my interpreter doesn't allow for it.

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

      It is set to 0 in rewind method explicitly 7:40

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

      Yes because rewind gets called at the very beginning

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

    In which video did you introduce Route code ?

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

      Search the playlist & you will find it. I think its 2.23, link to the outline repo is in the description

  • @sujithm.p9776
    @sujithm.p9776 2 роки тому

    I didn't get when we use iterator. What is the use case.

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

      When you want to make some class iterable, like a custom collection class as an example.

    • @sujithm.p9776
      @sujithm.p9776 2 роки тому

      @@ProgramWithGio okay. I understood there will be a collection of objects and we need to iterate over these objects in foreach this is useful. Apart from that what is the advantage? Is there any advantage while reading data from database? Performance wise?

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

      @@sujithm.p9776 yes if you are working on a large data set you can use iterators/generator to reduce memory consumption and not load everything in memory at once. We cover generators as well in this series.

    • @sujithm.p9776
      @sujithm.p9776 2 роки тому

      @@ProgramWithGio well I understood the scenario. But little curious to know whether laravel uses this concept?

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

      Yes, lazy collections use generators

  • @paulfontaine7819
    @paulfontaine7819 Місяць тому

    9:50 off topic: i would have used a trait for getIterator, leaving more freedom to inherit from another class..

    • @ProgramWithGio
      @ProgramWithGio  Місяць тому

      Sure, if you want to use the same code in other classes

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

    I tried the code and I still get the error "Fatal error: Uncaught Error: Cannot access protected property App\Lesson222\Invoice::$id". I'm I missing something?

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

      You may have copied code wrongly. Error says you are trying to access protected property. Share the full code if you can

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

      class Invoice {
      protected string $id;
      public function __construct(private float $amount) {
      $this->id = random_int(10000, 99999);
      }
      }
      class InvoiceCollection implements \IteratorAggregate {
      /**
      * @param Invoice[] $invoices
      */
      public function __construct(public array $invoices) {
      }
      public function getIterator(): Traversable {
      return new \ArrayIterator($this->invoices);
      }
      }
      //index.php
      $invoiceCollection = new InvoiceCollection([new Invoice(15), new Invoice(25), new Invoice(30)]);
      echo '';
      foreach ($invoiceCollection as $invoice) {
      echo $invoice->id.' - '. $invoice->amount. '';
      }
      echo '';@@ProgramWithGio

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

      thank you in advance!

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

    why 56 no video is private?

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

      It was deleted before it was published & new one was uploaded in its place. Forgot to remove it from playlist. Shouldnt be there anymore. Thanks for letting me know

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

    Fast and Furious PHP edition

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

    how the invoice collection class is calling these methods in this class nowhere in code you are calling them

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

      That's how iterators work, it gets called whenever you loop over a class that implements iterator methods

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

      @@ProgramWithGio thanks a lot for reply

  • @b.c.2177
    @b.c.2177 Рік тому

    This course is rather demonstration of your skeels than a course for learning PHP. You are talking non stop, like a machine, trying to impress the watchers, but people are not machines. So, it is vey hard to learn PHP watching such a course, even it looks nice and reach.

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

      I'm not trying to demonstrate anything or impress anyone. I've made editing mistakes in the beginning where I cut out empty spaces making it seem like I don't stop. You can easily slow it down on UA-cam by clicking the ⚙️ icon.
      As you can see from the comments, others find it helpful, so if it doesn't work for you feel free to watch something else. Cheers

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

    Thank you.