I came to this video because I have a need for local classes but I don't need the full 'class file' etc. This was exactly as I needed! It is explained really well and shows the advantages and limitations clearly. An excellent 8 minutes spent! Thank you. Appreciated. :) p.s. My 'use case' is that I have an array and I need to add a couple of functions to easily use the values of the array in testing. Anonymous classes are the perfect answer for this. :)
Yup, for me, the main use case has been with testing. Haven't really used it outside of that. But I've seen frameworks use anonymous classes outside of testing for example Laravel recently added anonymous classes support for its migrations.
I struggled with wrapping my head around Anonymous Functions so you can imagine how I felt when I reached this topic. But it was easier to understand this time. Plus the use case you talked about helped to put things in place in my mind. Thanks Gio.
Can you explain why did you use promoted properties in both constructors? I think it should be used only in parent constructor for clarity that you are not creating the same properties in Child Class.
Thank you for pointing it out & yes you are correct. I made a mistake while copying & pasting the parent's constructor arguments, there is no need to use property promotion inside the anonymous class when extending from a base class, simply accepting those as arguments is enough. That being said, the example right after that does not use inheritance would need the properties defined since we are passing down the arguments.
@@ProgramWithGio If I don't use parent's constructor arguments in the constructor of anonymous class then I get an error that: Fatal error: Uncaught ArgumentCountError: Too few arguments to functionApp\AnonymousClassA::__construct(), 0 passed in /var/www/app/AnonymousClassA.php on line 23 and exactly 2 expected in /var/www/app/AnonymousClassA.php:7 Stack trace: #0 /var/www/app/AnonymousClassA.php(23): App\AnonymousClassA->__construct() #1 /var/www/app/AnonymousClassA.php(19): App\AnonymousClassA@anonymous->__construct(1, 2) #2 /var/www/public/index.php(10): App\AnonymousClassA->bar() AnonymousClassA is name of my parent class.
I came to this video because I have a need for local classes but I don't need the full 'class file' etc.
This was exactly as I needed! It is explained really well and shows the advantages and limitations clearly. An excellent 8 minutes spent!
Thank you. Appreciated. :)
p.s. My 'use case' is that I have an array and I need to add a couple of functions to easily use the values of the array in testing. Anonymous classes are the perfect answer for this. :)
Super happy to hear this & glad it was useful. Thank you 💙
I was about to ask for the use cases, as they were discussed at the end. Very nice
Yup, for me, the main use case has been with testing. Haven't really used it outside of that. But I've seen frameworks use anonymous classes outside of testing for example Laravel recently added anonymous classes support for its migrations.
Anonymous classes are essential for tests. Thanks a million!
They do come in handy for sure
I struggled with wrapping my head around Anonymous Functions so you can imagine how I felt when I reached this topic. But it was easier to understand this time. Plus the use case you talked about helped to put things in place in my mind. Thanks Gio.
Awesome 👍. You're welcome & thank you 🙌
Great video did not know about anonymous classes
Perfect explanation as always 👏
Thank you
Kind of mind blown by the use of anonymous classes in testing.
They do come in handy
you know that u are the best right !!
Thank you 💙
thank you for this lesson
You are welcome!
Can you explain why did you use promoted properties in both constructors? I think it should be used only in parent constructor for clarity that you are not creating the same properties in Child Class.
Thank you for pointing it out & yes you are correct. I made a mistake while copying & pasting the parent's constructor arguments, there is no need to use property promotion inside the anonymous class when extending from a base class, simply accepting those as arguments is enough. That being said, the example right after that does not use inheritance would need the properties defined since we are passing down the arguments.
@@ProgramWithGio If I don't use parent's constructor arguments in the constructor of anonymous class then I get an error that: Fatal error: Uncaught ArgumentCountError: Too few arguments to functionApp\AnonymousClassA::__construct(), 0 passed in /var/www/app/AnonymousClassA.php on line 23 and exactly 2 expected in /var/www/app/AnonymousClassA.php:7
Stack trace:
#0 /var/www/app/AnonymousClassA.php(23): App\AnonymousClassA->__construct()
#1 /var/www/app/AnonymousClassA.php(19): App\AnonymousClassA@anonymous->__construct(1, 2)
#2 /var/www/public/index.php(10): App\AnonymousClassA->bar() AnonymousClassA is name of my parent class.
Thank you very much, great video as usual
You're welcome, thank you
Gio, how do you insert "public" to 3 parameters in __construct simultaneously? What PHPStorm shortcut it is?
Its multicursor editing, alt + shift + click. Should work in most code editors
@@ProgramWithGio Thank you, teacher
Thank you.
💙
Thnak you
You're welcome
👍👍👍👍👍👍👍👍👍
🙏🙏
Anonymous classes just support in php v8.* ?
No, anonymous classes was added in PHP 7