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
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.
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?
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.
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)
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?
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
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 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?
@@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.
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?
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
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.
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
i have been using youtube for 15 years to study , hands down your the best teacher , tha a man can hope for
Thank you 💙
Agreed 100%!
Hands down, this is the best explanation of Iterators I've even seen. Thanks!
Happy to hear, thank you 💙
Jaw drop, I had no friggin clue something like this was possible. This is the best PHP course I've ever seen.
Thank you 🙏
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
Thank you 💙💙
I'm really enjoying this series and learning more
Respect from Pakistan.
Glad to hear that, thank you 💙💙
This the best class about iterators with examples. Keep it up will done.
Thank you
Extremely simple explanation
Thank you so much for sharing this content of excellent quality.
You're welcome 🙌
i love you bro thank you so much for all those videos clear and simple example that make it easy.
Glad you like them, thank you 💙💙
Great lesson, I've bagged another one (need to practice though). Thanks alot Gio.
Awesome, thank you 💙 keep going
Thank you for this lesson!
You're welcome
instead of implementing iterator, why not just use nested foreach loop?
or you just did it to show the implementation of iterator?
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.
Thank you Gio
You're welcome
great lesson. thanks
You are welcome
Thank you GIO:)
You're welcome
amazing 🌟
Thank you! 😊
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?
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.
Great lesson, thank you!
Thank you for tor sharing this, great video!
Glad you enjoyed it, thank you 💙
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
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)
@@ProgramWithGio Error was incompatible interface, so i got rid of return stmt and it worked.
I dont know any of that topic. Thanks!!!!!!
🙌🙌
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?
What is the issue that you are having, can you share more details?
Thanks!
No problem!
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
Hello, add mixed as return type to your key method.
@@ProgramWithGio thank you very much it works
Great lesson, it would be greater if you add an iterator example of linked list? Thank you :)
Thank you & thanks for the suggestion 💙
Thank you.
You're welcome 👍
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.
It is set to 0 in rewind method explicitly 7:40
Yes because rewind gets called at the very beginning
In which video did you introduce Route code ?
Search the playlist & you will find it. I think its 2.23, link to the outline repo is in the description
I didn't get when we use iterator. What is the use case.
When you want to make some class iterable, like a custom collection class as an example.
@@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?
@@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.
@@ProgramWithGio well I understood the scenario. But little curious to know whether laravel uses this concept?
Yes, lazy collections use generators
9:50 off topic: i would have used a trait for getIterator, leaving more freedom to inherit from another class..
Sure, if you want to use the same code in other classes
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?
You may have copied code wrongly. Error says you are trying to access protected property. Share the full code if you can
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
thank you in advance!
why 56 no video is private?
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
Fast and Furious PHP edition
You can slow it down on UA-cam buddy 👍
how the invoice collection class is calling these methods in this class nowhere in code you are calling them
That's how iterators work, it gets called whenever you loop over a class that implements iterator methods
@@ProgramWithGio thanks a lot for reply
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.
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
Thank you.
💙