PHP Anonymous Classes - Full PHP 8 Tutorial

preview_player
Показать описание
In this lesson, you will learn what anonymous classes are in PHP, how to create & use them. Anonymous classes like anonymous functions are nameless. You are able to create one-off objects using anonymous classes. You can either assign an anonymous class to a variable that will simply give you the object of that anonymous class, or just pass it as an argument since it's just an expression.

Note that at 4:54 I copied constructor promoted properties & pasted them into the anonymous class. Promoted properties should not be used inside an anonymous class when you are extending from a base class since those already exist on the base class.

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
THANK YOU!

LESSON 2.15

RESOURCES
Рекомендации по теме
Комментарии
Автор

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. :)

ryanqvincent
Автор

I was about to ask for the use cases, as they were discussed at the end. Very nice

parijke
Автор

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.

Zubbee
Автор

Anonymous classes are essential for tests. Thanks a million!

Vitalii-mr
Автор

Great video did not know about anonymous classes

federicobau
Автор

Kind of mind blown by the use of anonymous classes in testing.

ItsDigiUtime
Автор

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.

adrianez-programuj
Автор

Thank you very much, great video as usual

karam
Автор

Gio, how do you insert "public" to 3 parameters in __construct simultaneously? What PHPStorm shortcut it is?

aerohcss
Автор

Anonymous classes just support in php v8.* ?

mohammadkhazaee