PHP Attributes - Simple Router With Attributes - Full PHP 8 Tutorial

Поделиться
HTML-код
  • Опубликовано: 9 фев 2025
  • Attributes feature that was introduced in PHP 8.0 may sound complicated but it's pretty simple when you put it into practice. In this video, we explore the attributes feature & refactor our simple router to use attributes.
    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
    ⭐ Become a Patreon: / programwithgio
    THANK YOU!
    🛠️ TOOLS & SERVICES I USE
    Digital Ocean Hosting - Get 100$ credit - m.do.co/c/38b9...
    Domains on Namecheap - namecheap.pxf....
    Envato Elements - 1.envato.marke...
    Adobe Photoshop & Premiere - clk.tradedoubl...
    COURSE PLAYLIST
    • Learn PHP The Right Wa...
    RESOURCES
    Source repo - github.com/gge...
    Docs - www.php.net/ma...
    RFC that added attributes support - wiki.php.net/r...
    Bitwise operators lesson - • PHP Operators Part 2 -...
    Reflection API - • Dependency Injection C...
    ** Affiliate Disclaimer: Some of the above links may be affiliate links, which may generate me a sales commission at no additional cost to you.

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

  • @aeiou...
    @aeiou... Год назад +8

    I was planning on buying a PHP course on udemy because I want to enhance my php knowledge. I'm a laravel developer and I feel like I already forgot the most part of pure php. I'm glad I found this series, and its FREE!! Thank you!

  • @aymanaoun2136
    @aymanaoun2136 2 года назад +1

    For being an awesome technical teacher!

  • @hansschuijff
    @hansschuijff 2 года назад +12

    A more difficult subject to get into, since the php doc itself that you showed at the start of the video, is not very explanatory or clear about what attributes are and how they work.
    After seeing this lesson through and making the effort to code along with you I am now confident that I understand it enough to be able to use it.
    What made it clear for was that you showed that Attribute is just another class you can use and extend by creating your own Attribute classes.
    In the end it didn't seem difficult anymore and that shows me you're a great teacher.
    Thanks, Gio. Top job.

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

      I'm really happy to hear that, thank you so much 💙

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

    This course is extremely useful if someone would like to dive right into PHP. Thanks a million!

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

    This is the best attributes explanation! Thank you for sharing!

  • @gaborjonas2740
    @gaborjonas2740 3 года назад +17

    Quality, modern PHP stuff, keep it up.

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

    Thanks!

  • @benderbg
    @benderbg 5 месяцев назад

    The grind is real.

  • @reza.kargar
    @reza.kargar Год назад

    You just described the topic perfectly 👌🏻🙏🏻

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

    i love you man !!! thank you so much

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

    Wow, this one was pretty cool. I'm like seeing how we are advancing and yet you always seem to say that it can't be used in production... and I'm like, men there must be a ton of things to learn. I followed and have updated my router class. Thanks Gio. Awesome. I'm moving on...

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

      Its not about learning ton of new things, its that this codebase is for education only, it lacks some important features that you would get from a full scale framework like Laravel or Symfony.

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

      @@ProgramWithGio Ok. I think when I start looking into these frameworks I'll understand more of what you are saying. Notwithstanding, I'm truly learning alot

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

    Really well made video 👏

  • @iambasanta
    @iambasanta 3 года назад +4

    This whole tutorial playlist is outstanding. Learning a lot of things from you. Thank you !!!
    Would you make a video on when to use a framework , If the problems can be solved with vanilla PHP then is it worth to do it with framework? I know framework increases the development speed. But using it for smaller projects ? What are your opinions I would love to hear it.

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

      Thank you 💙. Yes I'll make a video about that, can't give an exact time but towards the end of the series probably. My opinion is that unless the app is very small, using a framework will only speed up the work. I personally use Laravel for pretty much everything.

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

      @@ProgramWithGio Laravel

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

    hmm, you are awesome.
    other speakers tell about his vacation, mood, dogs and at the end about php attributes )))
    thank you

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

    Quality stuff! Thanks!! :)

  • @ЯоКуассиЖан-Клод

    Thanks you very very much! you are the best)

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

    im really amazed the information you have how did you get all these knowledge please !

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

      Thank you. I don't know everything by heart. A lot of research & preparation goes into each video, for example this video took me over 15 hours to make including editing.

    • @drcod3r329
      @drcod3r329 3 года назад +1

      @@ProgramWithGio
      i see that im just really amazed your channel deserves alot and also if possible do a laravel short videos and tips.

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

    Realy how Could this playlist are for free!!!! God save you . And realy we are apperciate your effort thaks alot

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

    Amazing video as always!!! Please could you create a version two of this video with parameters. It's something i just can't get my head around. Some store it in a $request->getParams(), others have it public function($param1, $param2) etc. Would be extremely helpful. Thankyou!!!! :)

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

      Thank you 🙏. We use SlimPHP later in the series which comes with built in router and param support so you won't have to build it from scratch

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

      @@ProgramWithGio Yes i saw that video too. Sadly it didn't help me learn the underlying technology in how dynamic routers with parameters work. Thanks tho :)

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

    Hello, you make really great videos and I look at them with great enthusiasm. But one thing I miss here, namely: independent of a 3rd framework (such as Slim), how could you form a group here with the router class? I lack this here and also the know-how for it. I would be very happy if you could also make a separate video for this. Thank you in advance.

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

      Thank you, and thanks for the suggestion, I'll add it in my list of videos to make 🙌

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

      @@ProgramWithGio Thank you, I'm really looking forward to it. Do you know roughly how long it might be according to your calendar? Because then I can explicitly adapt to it.

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

      @@MrLion22 no idea honestly, I'm pretty swamped right now with work and videos. Gotta finish PHP series and then Laravel. So can't give you a rough ETA on it unfortunately

    • @MrLion22
      @MrLion22 2 года назад +1

      @@ProgramWithGio OK, I can understand that. Then I'll just let myself be surprised and just wait and see. Hope it comes out soon. Thank you and for the quick reply ;-)

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

    Bro, i have one problem. At timestamp 12:23, I set the attribute "#[Route('/examples/generator')]". This works fine when the url has "/examples/generator" but fails when the url has the ending "/" like "/examples/generator/". I tried defining 2 attributes with the second one having the ending "/" and yet it failed stating i cannot have duplicate attributes. How do i address this problem?

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

      You could modify the Router class (resolve method) and trim of any trailing slashes

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

      @@ProgramWithGio Bro, the problem resolved itself when we defined the attribute as repeatable. Then i defined 2 routes. It worked. But the trimming the trailing slashes would be a better option. Tq so much.

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

    RegisterRoutesFromControllerAttributes has a nested loop it can be very slow if you have many routes + many http requests. This method is called in every http request right?

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

      Yes, there needs to be a caching layer for a more optimal solution. This is just an example demonstrating how attributes can be used for routing.

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

    For simplesity and perfermance, In this case of routing I prefer to include a config file return array of all routes and merge it with array variable in the Route::class

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

      For sure, its fine if you have a few routes but as you add more routes then extracting it to a config routes file is the better way. Great tip 👍

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

      @@ProgramWithGio
      Yes, suppose we have a folder named routes contains: web.php , api.php and admin.php
      We can load it easy using this loop:
      foreach (glob(__DIR__ .'/routes/*.php') as $route) {
      require_once $route;
      }

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

    Hey Gio. I see that you declare strict types by doing something on "

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

      What do you mean? If you mean the autocomplete part thats just the IDE. Also I have it set up so that it adds strict types to all new PHP classes created within the IDE

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

      @@ProgramWithGio yup, I mean IDE set up that adds strict types to all new PHP classes. What do you use for that?

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

      ​@@bakosyyit's just a setting on PHPStorm. I believe it's a PHP stub template file. You can Google it and find how to edit it on PHPStorm

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

    Do i need to understand reflection Api before I can use Attributes

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

      If you want to make custom attributes & functionality then yes. If you want to use built in attributes or attributes that come with frameworks/package then not really

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

      @@ProgramWithGio ok so I'm beginner in php do i need to know about this before i can start learning some framework?

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

      @@codewithsheikh2805 there are other topics you should be familiar with before moving into frameworks. Attributes isn't really necessary for that. Best way to know is to start the framework and if you get stuck or don't understand something, then learn about that specific topic and do it in parallel.

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

      @@ProgramWithGio thanks bro.

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

    gI have made a conditional listing in this coding, but what I want to do is to find the selected data from the database and have it listed, for example, when the pediatric service is selected, the patients hospitalized in the pediatric service will be listed.

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

      I can't really help you without knowing what the problem is or having access to the code.

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

      @@ProgramWithGio let me assign the code

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

      What I want to do is to list the data in the selectbox, whichever is selected, from the database.
      for example child service is selected in selectbox
      I want to list the patients treated in the pediatric service to the screen.

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

    your job is very great. appreciated you. but a lot of things we not understand. if I compare with other programmers, you are tackle a problem with complex method. I don't know if I have the basic skills of php or your method is complex? please explain. but don't mind bro

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

      I'm not sure what you mean. What problem did I tackle with complex method? Attributes is not complex, it's a feature that I'm explaining and providing a usecase. If it seems complex then you can start from first section maybe, are you following the series from the beginning?
      Note that this is third section of the series which is advanced PHP, so if it seems hard I would advise to watch the series from the beginning. There are a lot of videos in first & second sections

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

    Can we expect "Laravel the right way" with some awesome project ?

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

      🙂. I don't know yet, after this my plan is Laravel & Shopify. We'll see

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

    What if controller class have more than one method , how $method will get the right method of controller class , video reference 10:44

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

      Because route is defined in attribute & the method name is there on the controller method definition, keep watching & at around 12:00 you will see multiple methods within controller

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

      @@ProgramWithGio Got it , Thanks Sir

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

    do you think about making a laravel course too? also thank you for the videos ur a god

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

      Thank you. After PHP series is over I'll be doing more Laravel content, not a full course like PHP but more project oriented

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

    Will you cover AJAX?

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

      Not directly since it's not a PHP thing. Might use it in a project though

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

    Can we generate getters & setters functions for properties using Attributes?
    Ex:
    #[Getter]
    #[Setter]
    private $somePropertyName;
    This should generate the following functionality:
    getSomePropertyName(){...}
    setSomePropertyName(){...}

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

      Not sure to be honest. You can use IDEs for that to generate getter & setters. If you mean dynamically then you can use magic __get & __set methods. Not sure I understand the use case.

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

      @@ProgramWithGio Isn't using __get and __set method bad practice?

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

      @@codewithsheikh2805 depends on the use, there are pretty good usecases for it

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

    👍

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

    Sooooo, #Attributes are for PHP what @Decorators are for Typescript ?🤔🤔🤔🤔

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

      I don't know Typescript but from quick Google it seems yes

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

      ​@@ProgramWithGio You seem like the type to like type hinting so I suggest that you give it a try 👍

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

      @@ntdash2153 Thanks. I will when I have the need for it, I mainly work on back-end currently.

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

    its like python decorator.. also like flask

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

      Maybe 🤔. I'm not familiar with Python decorators or flask so not 100% sure.

  • @donnyturizo111
    @donnyturizo111 10 месяцев назад

    Porque no ponen así pero en español

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

    like

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

    pleplease can you help me

  • @davidmutua6971
    @davidmutua6971 10 месяцев назад

    I wish you could have done a separate video without all the Route stuff. This is for people whom find this and just want to use Attributes. Found one ruclips.net/video/oSo4xbP6ZYo/видео.html&ab_channel=GaryClarke

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

      Well, in this playlist the Route part fits in nicely. You can use Attributes for many different scenarios, Routing is one of them & a good example as well

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

    This is a anti-pattern way of programming. Use interfaces for routable controllers instead and register it by calling the interface method. Much clearer. Using the reflection class is never a good way of programming.

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

      I respectfully disagree. Don't see this as antipattern, most popular frameworks use both attributes/annotations & reflection API so not sure what you mean?

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

      @@ProgramWithGio My main point is that there are other and clearer options.
      I observe that in practice the Reflection API is used too often and incorrectly, resulting in cluttered and poorly maintainable code.
      Please always try to find another solution first, typically with interfaces.

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

      @@bernhardd626 we didn't use it wrongly or poorly in this video. Reflection API exists for a reason and Attributes is a great feature which you can't really use without Reflection API. Doing routing via attributes/annotations is a very common practice & there is absolutely nothing wrong with it.
      As I said before most popular frameworks utilize Reflection API the same way and use it pretty often in their source.

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

    Hello Gio! Do you have Discord / Are you going to start one? I'd love to join it and learn from a very experienced programmer like yourself.

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

      Hello. Yes I have a discord, though I am not very active on it. You can find the link in channel about page.

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

      Was about to ask the exact same Q! Been looking for a good PHP community. Have had a great experience in Kevin Powell's for the front end stuff.

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

    Do you have instagram