PHP Anonymous Classes - Full PHP 8 Tutorial

Поделиться
HTML-код
  • Опубликовано: 29 окт 2024

Комментарии • 33

  • @ryanqvincent144
    @ryanqvincent144 Год назад +3

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

    • @ProgramWithGio
      @ProgramWithGio  Год назад +1

      Super happy to hear this & glad it was useful. Thank you 💙

  • @parijke
    @parijke 3 года назад +10

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

    • @ProgramWithGio
      @ProgramWithGio  3 года назад +2

      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.

  • @Vitalii-m6r
    @Vitalii-m6r Год назад

    Anonymous classes are essential for tests. Thanks a million!

  • @Zubbee
    @Zubbee 2 года назад +3

    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.

    • @ProgramWithGio
      @ProgramWithGio  2 года назад

      Awesome 👍. You're welcome & thank you 🙌

  • @federicobau8651
    @federicobau8651 2 года назад +2

    Great video did not know about anonymous classes

  • @shlxng1787
    @shlxng1787 Год назад +1

    Perfect explanation as always 👏

  • @ItsDigiUtime
    @ItsDigiUtime Год назад

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

  • @hossamragab1428
    @hossamragab1428 8 месяцев назад

    you know that u are the best right !!

  • @ilya_123__
    @ilya_123__ 11 месяцев назад

    thank you for this lesson

  • @adrianez-programuj3313
    @adrianez-programuj3313 3 года назад +5

    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.

    • @ProgramWithGio
      @ProgramWithGio  3 года назад +5

      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.

    • @amijazz1941
      @amijazz1941 Год назад

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

  • @karam0101
    @karam0101 2 года назад

    Thank you very much, great video as usual

  • @aerohcss
    @aerohcss Месяц назад

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

    • @ProgramWithGio
      @ProgramWithGio  Месяц назад

      Its multicursor editing, alt + shift + click. Should work in most code editors

    • @aerohcss
      @aerohcss Месяц назад

      @@ProgramWithGio Thank you, teacher

  • @memack101
    @memack101 2 года назад

    Thank you.

  • @Probablyaliq4
    @Probablyaliq4 Месяц назад

    Thnak you

  • @sym2574
    @sym2574 Год назад

    👍👍👍👍👍👍👍👍👍

  • @mohammadkhazaee9730
    @mohammadkhazaee9730 3 года назад

    Anonymous classes just support in php v8.* ?

    • @ProgramWithGio
      @ProgramWithGio  3 года назад

      No, anonymous classes was added in PHP 7