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

  Рет қаралды 26,493

Program With Gio

Program With Gio

2 жыл бұрын

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/38b935ad74e5
Domains on Namecheap - namecheap.pxf.io/rnRjdQ
Envato Elements - 1.envato.market/c/2937311/298...
Adobe Photoshop & Premiere - clk.tradedoubler.com/click?p=...
COURSE PLAYLIST
• Learn PHP The Right Wa...
RESOURCES
Source repo - github.com/ggelashvili/learnp...
Docs - www.php.net/manual/en/languag...
RFC that added attributes support - wiki.php.net/rfc/attributes_v2
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.

Пікірлер: 110
@aeiou...
@aeiou... 11 ай бұрын
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!
@ProgramWithGio
@ProgramWithGio 11 ай бұрын
Happy to hear, thank you 💙
@hansschuijff
@hansschuijff Жыл бұрын
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 Жыл бұрын
I'm really happy to hear that, thank you so much 💙
@gaborjonas2740
@gaborjonas2740 2 жыл бұрын
Quality, modern PHP stuff, keep it up.
@user-zh2oz9fe4m
@user-zh2oz9fe4m 11 ай бұрын
This course is extremely useful if someone would like to dive right into PHP. Thanks a million!
@ProgramWithGio
@ProgramWithGio 11 ай бұрын
Thank you 🙏
@kinvain
@kinvain Жыл бұрын
This is the best attributes explanation! Thank you for sharing!
@ProgramWithGio
@ProgramWithGio Жыл бұрын
Glad it was helpful, thank you
@aymanaoun2136
@aymanaoun2136 Жыл бұрын
For being an awesome technical teacher!
@ProgramWithGio
@ProgramWithGio Жыл бұрын
Thank you so much 💙💙💙
@reza.kargar
@reza.kargar Жыл бұрын
You just described the topic perfectly 👌🏻🙏🏻
@ProgramWithGio
@ProgramWithGio Жыл бұрын
Thank you
@chrisodillman3355
@chrisodillman3355 Жыл бұрын
i love you man !!! thank you so much
@feldinho
@feldinho 7 ай бұрын
Quality stuff! Thanks!! :)
@ProgramWithGio
@ProgramWithGio 7 ай бұрын
Glad you enjoyed it, thank you
@Markov39
@Markov39 9 ай бұрын
Really well made video 👏
@ProgramWithGio
@ProgramWithGio 9 ай бұрын
Thank you 🙌
@jesiooooo
@jesiooooo Жыл бұрын
GREATE COURSE
@ProgramWithGio
@ProgramWithGio Жыл бұрын
Thank you
@user-fy7qn9bv7y
@user-fy7qn9bv7y 7 ай бұрын
Thanks you very very much! you are the best)
@ProgramWithGio
@ProgramWithGio 7 ай бұрын
You're welcome, thank you 🙌
@hossamragab1428
@hossamragab1428 2 ай бұрын
Realy how Could this playlist are for free!!!! God save you . And realy we are apperciate your effort thaks alot
@ProgramWithGio
@ProgramWithGio 2 ай бұрын
You're welcome & thank you
@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 жыл бұрын
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
@iambasanta
@iambasanta 2 жыл бұрын
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 2 жыл бұрын
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 2 жыл бұрын
@@ProgramWithGio Laravel
@gabrielcarvalho7964
@gabrielcarvalho7964 2 жыл бұрын
Thanks!
@ProgramWithGio
@ProgramWithGio 2 жыл бұрын
You're welcome & thank you!
@alextc4199
@alextc4199 Жыл бұрын
hmm, you are awesome. other speakers tell about his vacation, mood, dogs and at the end about php attributes ))) thank you
@ProgramWithGio
@ProgramWithGio Жыл бұрын
heh, thank you 🙌
@drcod3r329
@drcod3r329 2 жыл бұрын
im really amazed the information you have how did you get all these knowledge please !
@ProgramWithGio
@ProgramWithGio 2 жыл бұрын
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 2 жыл бұрын
@@ProgramWithGio i see that im just really amazed your channel deserves alot and also if possible do a laravel short videos and tips.
@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 Жыл бұрын
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 :)
@sergeisurin2963
@sergeisurin2963 Жыл бұрын
👍
@ProgramWithGio
@ProgramWithGio Жыл бұрын
🙌
@alisson8320
@alisson8320 2 жыл бұрын
do you think about making a laravel course too? also thank you for the videos ur a god
@ProgramWithGio
@ProgramWithGio 2 жыл бұрын
Thank you. After PHP series is over I'll be doing more Laravel content, not a full course like PHP but more project oriented
@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; }
@MrLion22
@MrLion22 Жыл бұрын
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 Жыл бұрын
Thank you, and thanks for the suggestion, I'll add it in my list of videos to make 🙌
@MrLion22
@MrLion22 Жыл бұрын
@@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 Жыл бұрын
@@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 Жыл бұрын
@@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 ;-)
@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
@enderozata9438
@enderozata9438 2 жыл бұрын
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 2 жыл бұрын
I can't really help you without knowing what the problem is or having access to the code.
@enderozata9438
@enderozata9438 2 жыл бұрын
@@ProgramWithGio let me assign the code
@enderozata9438
@enderozata9438 2 жыл бұрын
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.
@_nirajghimire
@_nirajghimire 2 жыл бұрын
Can we expect "Laravel the right way" with some awesome project ?
@ProgramWithGio
@ProgramWithGio 2 жыл бұрын
🙂. I don't know yet, after this my plan is Laravel & Shopify. We'll see
@cirhanne
@cirhanne 8 ай бұрын
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 8 ай бұрын
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.
@tobiomotosho6849
@tobiomotosho6849 2 жыл бұрын
Will you cover AJAX?
@ProgramWithGio
@ProgramWithGio 2 жыл бұрын
Not directly since it's not a PHP thing. Might use it in a project though
@codewithsheikh2805
@codewithsheikh2805 Жыл бұрын
Do i need to understand reflection Api before I can use Attributes
@ProgramWithGio
@ProgramWithGio Жыл бұрын
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 Жыл бұрын
@@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.
@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 Жыл бұрын
You could modify the Router class (resolve method) and trim of any trailing slashes
@truthteachers
@truthteachers Жыл бұрын
@@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.
@bakosyy
@bakosyy 8 ай бұрын
Hey Gio. I see that you declare strict types by doing something on "
@ProgramWithGio
@ProgramWithGio 8 ай бұрын
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 8 ай бұрын
@@ProgramWithGio yup, I mean IDE set up that adds strict types to all new PHP classes. What do you use for that?
@ProgramWithGio
@ProgramWithGio 8 ай бұрын
​@@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
@KIKOmanasijev
@KIKOmanasijev Жыл бұрын
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 Жыл бұрын
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
@fafaratze
@fafaratze 2 жыл бұрын
its like python decorator.. also like flask
@ProgramWithGio
@ProgramWithGio 2 жыл бұрын
Maybe 🤔. I'm not familiar with Python decorators or flask so not 100% sure.
@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 Жыл бұрын
@@ProgramWithGio Got it , Thanks Sir
@seoonlyRU
@seoonlyRU 2 жыл бұрын
like
@ProgramWithGio
@ProgramWithGio 2 жыл бұрын
Thank you
@donnyturizo111
@donnyturizo111 4 ай бұрын
Porque no ponen así pero en español
@ProgramWithGio
@ProgramWithGio 3 ай бұрын
Not sure what you mean
@ntdash2153
@ntdash2153 Жыл бұрын
Sooooo, #Attributes are for PHP what @Decorators are for Typescript ?🤔🤔🤔🤔
@ProgramWithGio
@ProgramWithGio Жыл бұрын
I don't know Typescript but from quick Google it seems yes
@ntdash2153
@ntdash2153 Жыл бұрын
​@@ProgramWithGio You seem like the type to like type hinting so I suggest that you give it a try 👍
@ProgramWithGio
@ProgramWithGio Жыл бұрын
@@ntdash2153 Thanks. I will when I have the need for it, I mainly work on back-end currently.
@enderozata9438
@enderozata9438 2 жыл бұрын
pleplease can you help me
@ProgramWithGio
@ProgramWithGio 2 жыл бұрын
What do you need help with?
@davidmutua6971
@davidmutua6971 3 ай бұрын
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 kzbin.info/www/bejne/pYTSZauYhZuNj9E&ab_channel=GaryClarke
@ProgramWithGio
@ProgramWithGio 2 ай бұрын
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
@Wane-maxx
@Wane-maxx Жыл бұрын
How would you handle query and url parameters?, this is too much code for a simple custom router, which makes it stressful and by far, a tool for a production.This is why PHP is a nightmare and unreliable for building serious web applications.
@ProgramWithGio
@ProgramWithGio Жыл бұрын
Query Params can be gotten with GET sulerglobal so not sure what's the issue with that. Url param handling can be built multiple ways, regex, reflection API, etc. This of course is not for production use, it's just an example demonstration of how frameworks implement things. For production, there are plenty of routing libraries that exist & frameworks that provide such features, Symfony/Laravel included.
@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 Жыл бұрын
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 Жыл бұрын
@@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 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 2 жыл бұрын
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 2 жыл бұрын
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 2 жыл бұрын
Do you have instagram
@enderozata9438
@enderozata9438 2 жыл бұрын
whatsapp
@ProgramWithGio
@ProgramWithGio 2 жыл бұрын
Nope. You can reach me on Twitter
PHP Enums With Practical Examples - Full PHP 8 Tutorial
28:13
Program With Gio
Рет қаралды 25 М.
PHP Traits - How They Work & Drawbacks - Full PHP 8 Tutorial
31:41
Program With Gio
Рет қаралды 31 М.
World’s Largest Jello Pool
01:00
Mark Rober
Рет қаралды 102 МЛН
Каха заблудился в горах
00:57
К-Media
Рет қаралды 9 МЛН
Атрибуты в PHP. Какую пользу даёт применение attributes
16:23
PHP 8 3 Released
11:03
ThePrimeTime
Рет қаралды 104 М.
PHP Generators Explained - Full PHP 8 Tutorial
25:47
Program With Gio
Рет қаралды 30 М.
PHP doesn't suck (anymore)
10:48
Aaron Francis
Рет қаралды 202 М.
When to use Traits, Interfaces, and Abstract Classes in PHP
15:08
Andrew Schmelyun
Рет қаралды 17 М.