Thank you so much for the amazing PHP courses you share online! Your tutorials have been a game-changer for me, and I can't express how much I’ve learned from your clear and practical explanations. Your passion for teaching and your ability to simplify complex concepts have inspired me to keep growing as a developer. I'm so grateful for all the effort and dedication you put into helping people like me learn and improve. Keep up the fantastic work-you’re making a real difference in the lives of so many aspiring developers!
I started watching and learning from your videos in this playlist about 1-1.5 months ago. Some topics in some videos were very difficult for me to understand and remember. However, this quick overview of Composition vs Inheritance was one of the videos that I found really helpful and easy to understand. Thank you!!
I really liked this lesson with slides and just explaining the code. Seems like it saves you time from typing so you can cover a lot more information. Using gaming terminology to explain concepts is a great idea.
Very nice video! I'm glad that some people take PHP seriously - most of them (I think) just remember the PHP3-4 days, where it was kinda like Javascript. One big note for everybody who is watching this, If you are working in finance territory (like invoices in the example), NEVER use the basic math operations, use the bc_math extension, or use "Money for PHP" for currencies! The reason is that default float point operations are not precise enough, and especially if you are exchanging, you'll get 1-2 cent differences because of rounding errors
On the last example where you were talking about composition being more useful than inheritance with things like pets or monsters both needing an attack method, I agree with that line of thinking but I think in that particular situation it would be better to use Traits rather than injecting more parameters into the constructors. The reason why is because this has the potential to sprawl out into a very intense amount of composition over time, and it becomes rather complicated to manage class instantiations that require a growing number of compositional inclusions. Traits allow you to embed that compositional functionality directly into the right class without retroactively editing the object instantiations at all the places you have used that class call in the code.
Yes & no. Traits sort of give illusion of composition while not exactly being composition. When it comes to testability, the real composition through construction injection is the right way. That being said, using traits is also a good way to achieve somewhat the same end-goal.
@@ProgramWithGio I thought you would ended up with Interfaces, what do you think about it? Could it work? Though if I try to figured it out, it would look a lot like using traits.
What an great video, understood every word by just watching once. Althoug I am always having difficulties when the examples are on sales,invoices or payments but the one about npcs are the best one that I got. Thank you GIO, gonna search more about compostition vs inheritance.
This is my 80th video! As I'm tending towards wanting to pat myself on the back, I can't help but imaging how much more the person who created from 1 to this 80th video I'm on all for us to learn pro bono should be patted. I appreciate Gio. God bless. About this lesson, when I first came across the inheritance lesson, I really thought it as a cool concept but as time went I saw how it can get complex very fast. So composition seems like it's easier to manage. But I've noted what you said about there still being some use cases for it. Thanks for this lesson Gio.
That is awesome, you & anyone who watches this many videos deserve a big pat on the back. So congrats & great job, we can celebrate 100 videos together when I publish 100th video in the series :)
Fair. I do mention how you would implement it using composition, just make classes for each of those attributes & inject them in the constructor. It can be a good practice, you can share the code after and I can review
Great video, love the "management has a great idea". It's legitimately a real world scenario. I never mind new ideas, but the worst is when there is a lack of foresight or when the requirements change half way through and there's "no time" to refactor properly. For use of composition, what's your thought on passing a reference versus the value (or) copy of the passed object. I've found in PHP it's smart enough to note recursion if it occurs when printing an object. In my case I was writing a lighting controller where a bridge has a list of bulbs, and the bulb is assigned to/requires a connection to the bridge. The bulbs have properties and methods and extend a base class, but some methods are proxied back to the bridge which also extends a base class (as you can have more than one type of bridge and type of light). Great video. Thanks!
After your explanation and agreeing with you on the uncertaincy of the direction of future changes, I now see that in most cases composition would probably be the better solution. I've also seen factory based solutions in WordPress plugins, so perhaps there are more solutions, but this is one. Thanks.
Yup, I used to use inheritance a lot in the past & it makes extension more difficult even though the keyword used there is "extends" :D. Composition is better in most cases.
Thank you. You can use traits for sure, now does traits mean composition? That's debatable. In my opinion traits != composition because composition means class is composed of objects, when you use traits you dont have the reference to the object, it simply just pastes in the code in your main class. It is good for code re-use & allows for horizontal code re-use but I would not call it composition as tempting as it might sound.
Fantastic bro. I love minute of this video. A lot to grasp about PHP which i now realize its really a full fledge language and perhaps better than many I have seen. The main point here is we should never compare the likes of JS framework and language with PHP, especially 8.x. The only setback with PHP is that its performance concerns when used in large batch processing as compared to the likes of Java and Python. Appreciate your feedback on this. Tq.
PHP's performance is not bad, it outperforms Python in some ways. There are many ways to improve performance of PHP & usually in most cases the bottleneck is the database & not PHP.
As usually great video, please keep on going. I hope some day you'll get somewhere into CI/CD scope. Please be the first one talking about pro things 😃
Thank you, I personally don't enjoy CI/CD stuff but we'll see, maybe one day. That being said we'll deploy the project that we are going to work on in this series
@@ProgramWithGio you could create a course on it if you don't want to give it for free...each driver could have its own video. anyway, it was just a suggestion. thank you for your work so far.
Thank you. I replied to the other comment about traits so I'll copy it here: You can use traits for code re-use, now does traits mean composition? That's debatable. In my opinion traits != composition because composition means class is composed of objects, when you use traits you don't have the reference to the object, it simply just pastes in the code in your main class. It is good for code re-use & allows for horizontal code re-use but I would not call it composition as tempting as it might sound.
Great video and topic. Question tho, does dependecy injection means composition? You where using DI when you make use of composition which makes composition looks like a "concept" and to "implement" it is by using DI, just like polymorphisms is the concept and interface is the implementation.
Nice video, you coulded explained traits in this video too. I know is not a big feature in PHP, but once I was trying to make most abstract classes for my models, puting all getter and setter in this classes, like class BaseModel { public function getId(): int { } //... } class DateTimeBaseModel extensa BaseModel { // Get set for created_at and updated_at } It works, until I need to chance a id collomn to bigint, and break all the inherance, I need to create new classes with return string for getId (of course, this was before PHP 8). So, a fill years ago I needed to upgrade PHP from 5.6 to 7.2, reading the broken changings, I learn traits, I think, this is a better solution, for this example. Like Trait GetterSetterIdInteger { private int $id; //Get set } class Model { use GetterSetterIdInteger; use GetterSetterDateTime; }
Thank you. There is a lesson about Traits in the second section. Traits don't really achieve the same thing as composition, yes it lets you reuse the code but that's about it. It shouldn't be used in place of composition in my opinion. It's better to pass down dependencies via constructor rather than include them within the same class using traits.
@@ProgramWithGio It's interesting, in your video game example I would definitly have gone with traits and interfaces. Like CanAttackTrait (with implementation) and CanAttackInterface for the public contract. This allows you to type hint in the functions with the interface while avoiding duplication of code. Even more in 8.1 with the intercetion type where we can say CanAttackInterface&CanMoveInterface. And that makes more sense in my opinion than having something like an AttackerService. What do you think? And why would you not call that composition ? Thanks for the videos, this is very interesting even for someone who has been developing in PHP professionally for several years like me.
@@flo_111 Traits give you the feel of composition but essentially it just puts the code in the class, as far as testability goes its better to have dependencies injected in constructors to achieve true composition. Now it also depends on preference, Laravel likes to use traits to achieve composition & as long as its used properly I think its fine. I personally prefer dependencies injected in constructors. Interfaces + traits is not a bad option.
Wow, I can't believe I've come this far! Master Gio, I have a question for you. I've watched all the lessons up to this point, and I've written every line of code you've written. I've completed all the exercises so far. However, I'm having trouble memorizing everything with 100% efficiency. Some information is starting to slip my mind. Since your course is the best, I don't see the point in watching other PHP courses (although I could be mistaken). So, after finishing your course and learning the basics of Laravel, I think it will be time to start applying for PHP developer jobs. As the next step in my PHP/some framework learning, I believe working with the original documentation would be beneficial. Could you give me some advice on how to properly work with documentation during my workflow? I mean, I don't think it's like reading Dostoevsky page by page, chapter after chapter (but I could be mistaken again). I think it would be a great idea for a new lesson: "How to effectively use PHP documentation to learn PHP the right way." 😄
That's awesome, good job. You shouldn't memorize anything, cause then it's expected to forget things. I also don't remember a lot of the things all the time. The key thing is to look it up when in need. Once you are exposed to the idea and have practiced and know what it is about, when you come across it 3-4 months later it will kick in and come back to you from memory. As far as documentation, I wouldn't recommend just reading through all the documentation, instead you should read the pages of the things that you need for the project you are working on.
Can you say dependency injection is the same as composition ? Or is there something else that sets them apart. Honestly never heard it being called composition. And what about traits? Don't they go against code duplication also? Couldn't you add attack to a trait and let it be used by the monsters and the pets?
Composition is achieved by dependency injection. Of course you can instantiate the dependencies in constructor for example to achieve composition but dependency injection is better & we covered that earlier in this series. About the traits I'll paste in the answer I gave to others: You can use traits for code re-use, now does traits mean composition? That's debatable. In my opinion traits != composition because composition means class is composed of objects, when you use traits you don't have the reference to the object, it simply just pastes in the code in your main class. It is good for code re-use & allows for horizontal code re-use but I would not call it composition as tempting as it might sound.
Why **should** it be a trait? What's wrong with it being a class? Also note that this is just an example of inheritance vs composition lesson. Doing composition by injecting dependencies in constructor is the right way to do it instead of traits, makes writing tests easier.
All the courses that I watched about php explain php itself , but gio teaches you how to be good programer using php.
A big thank you to you
Glad you like the videos, thank you 💙
Thank you so much for the amazing PHP courses you share online! Your tutorials have been a game-changer for me, and I can't express how much I’ve learned from your clear and practical explanations.
Your passion for teaching and your ability to simplify complex concepts have inspired me to keep growing as a developer. I'm so grateful for all the effort and dedication you put into helping people like me learn and improve.
Keep up the fantastic work-you’re making a real difference in the lives of so many aspiring developers!
I started watching and learning from your videos in this playlist about 1-1.5 months ago. Some topics in some videos were very difficult for me to understand and remember. However, this quick overview of Composition vs Inheritance was one of the videos that I found really helpful and easy to understand. Thank you!!
Glad you like them! Thank you 🙌
I really liked this lesson with slides and just explaining the code. Seems like it saves you time from typing so you can cover a lot more information. Using gaming terminology to explain concepts is a great idea.
Glad you liked it, thank you
One of the best classes i've seem, good work!
Thank you
Very nice video! I'm glad that some people take PHP seriously - most of them (I think) just remember the PHP3-4 days, where it was kinda like Javascript.
One big note for everybody who is watching this, If you are working in finance territory (like invoices in the example),
NEVER use the basic math operations, use the bc_math extension, or use "Money for PHP" for currencies!
The reason is that default float point operations are not precise enough, and especially if you are exchanging, you'll get 1-2 cent differences because of rounding errors
Yup thats the way but for smaller projects where fractions don't matter much floats are fine. As long as you don't compare them directly for equality.
On the last example where you were talking about composition being more useful than inheritance with things like pets or monsters both needing an attack method, I agree with that line of thinking but I think in that particular situation it would be better to use Traits rather than injecting more parameters into the constructors. The reason why is because this has the potential to sprawl out into a very intense amount of composition over time, and it becomes rather complicated to manage class instantiations that require a growing number of compositional inclusions. Traits allow you to embed that compositional functionality directly into the right class without retroactively editing the object instantiations at all the places you have used that class call in the code.
Yes & no. Traits sort of give illusion of composition while not exactly being composition. When it comes to testability, the real composition through construction injection is the right way. That being said, using traits is also a good way to achieve somewhat the same end-goal.
@@ProgramWithGio I thought you would ended up with Interfaces, what do you think about it? Could it work?
Though if I try to figured it out, it would look a lot like using traits.
@@vegekou100 interfaces & dependency injection is the right way
Thank you, Gio.
Thank you 💙💙
hahaha, family members, or even your cat. This guy is great at explaining and funny
Glad you enjoyed it 😂
What an great video, understood every word by just watching once. Althoug I am always having difficulties when the examples are on sales,invoices or payments but the one about npcs are the best one that I got. Thank you GIO, gonna search more about compostition vs inheritance.
That's awesome, great job & thank you
Wow! This explanation is pure gold! Thank you for spending time on making this video, really appreciate it!
Glad it was helpful & that you liked it. Thank you 💙💙
This is my 80th video! As I'm tending towards wanting to pat myself on the back, I can't help but imaging how much more the person who created from 1 to this 80th video I'm on all for us to learn pro bono should be patted. I appreciate Gio. God bless. About this lesson, when I first came across the inheritance lesson, I really thought it as a cool concept but as time went I saw how it can get complex very fast. So composition seems like it's easier to manage. But I've noted what you said about there still being some use cases for it. Thanks for this lesson Gio.
That is awesome, you & anyone who watches this many videos deserve a big pat on the back. So congrats & great job, we can celebrate 100 videos together when I publish 100th video in the series :)
@@ProgramWithGio Oh wow. It's going to get to 100? Goodie!!! Well done Gio! I look forward to that.
16:30 this was going great I wish you have converted this to composition example in the video too.
Fair. I do mention how you would implement it using composition, just make classes for each of those attributes & inject them in the constructor. It can be a good practice, you can share the code after and I can review
@@ProgramWithGio This leads onto 'Dependency Injection containers?
@@ryanqvincent144 yup
Great teacher you are
Thank you
Great video, love the "management has a great idea". It's legitimately a real world scenario. I never mind new ideas, but the worst is when there is a lack of foresight or when the requirements change half way through and there's "no time" to refactor properly.
For use of composition, what's your thought on passing a reference versus the value (or) copy of the passed object. I've found in PHP it's smart enough to note recursion if it occurs when printing an object. In my case I was writing a lighting controller where a bridge has a list of bulbs, and the bulb is assigned to/requires a connection to the bridge. The bulbs have properties and methods and extend a base class, but some methods are proxied back to the bridge which also extends a base class (as you can have more than one type of bridge and type of light).
Great video. Thanks!
Thank you 🙌. I avoid references as much as possible, it makes code harder to debug & increases possibility of some hidden/magical bugs.
After your explanation and agreeing with you on the uncertaincy of the direction of future changes, I now see that in most cases composition would probably be the better solution. I've also seen factory based solutions in WordPress plugins, so perhaps there are more solutions, but this is one. Thanks.
Yup, I used to use inheritance a lot in the past & it makes extension more difficult even though the keyword used there is "extends" :D. Composition is better in most cases.
great explanation
Thank you 🙌
Great Explanation
Glad it was helpful, thank you
Great video, thank you 🙏
can we implement composition without dependency injection? maybe with traits?
Thank you. You can use traits for sure, now does traits mean composition? That's debatable. In my opinion traits != composition because composition means class is composed of objects, when you use traits you dont have the reference to the object, it simply just pastes in the code in your main class. It is good for code re-use & allows for horizontal code re-use but I would not call it composition as tempting as it might sound.
Fantastic bro. I love minute of this video. A lot to grasp about PHP which i now realize its really a full fledge language and perhaps better than many I have seen. The main point here is we should never compare the likes of JS framework and language with PHP, especially 8.x. The only setback with PHP is that its performance concerns when used in large batch processing as compared to the likes of Java and Python. Appreciate your feedback on this. Tq.
PHP's performance is not bad, it outperforms Python in some ways. There are many ways to improve performance of PHP & usually in most cases the bottleneck is the database & not PHP.
@@ProgramWithGio Very true. We need to optimize the queries before injecting into the PHP code.
Great examples, thanks!
Glad it was helpful
As usually great video, please keep on going. I hope some day you'll get somewhere into CI/CD scope. Please be the first one talking about pro things 😃
Thank you, I personally don't enjoy CI/CD stuff but we'll see, maybe one day. That being said we'll deploy the project that we are going to work on in this series
Nice! Thanks a lot.
You're welcome
Thank you. You explained very accessibly.
Glad it was helpful!
another amazing tuto :)
Thank you
Thank you.
You're welcome!
a tutorial on queues in php would be great.
Will try to include that in this series
@@ProgramWithGio that would be fantastic. there's not much relevant info on youtube about this topic.
@@ProgramWithGio also, you could cover multiple drivers (filesystem, db, fifo pipe).
@@Andu1991 not sure I can do that in this series. Maybe separate short series
@@ProgramWithGio you could create a course on it if you don't want to give it for free...each driver could have its own video.
anyway, it was just a suggestion.
thank you for your work so far.
Thanks, great examples and explanation. Where do traits fit in?
Thank you. I replied to the other comment about traits so I'll copy it here: You can use traits for code re-use, now does traits mean composition? That's debatable. In my opinion traits != composition because composition means class is composed of objects, when you use traits you don't have the reference to the object, it simply just pastes in the code in your main class. It is good for code re-use & allows for horizontal code re-use but I would not call it composition as tempting as it might sound.
amazing content!
Thank you
A thousand thanks for these examples, btw will you show some examples how to work with cURL later in the course?
Yup we'll cover curl and basics of APIs
Great video and topic.
Question tho, does dependecy injection means composition? You where using DI when you make use of composition which makes composition looks like a "concept" and to "implement" it is by using DI, just like polymorphisms is the concept and interface is the implementation.
DI is a way to implement/achieve composition.
Nice video, you coulded explained traits in this video too.
I know is not a big feature in PHP, but once I was trying to make most abstract classes for my models, puting all getter and setter in this classes, like
class BaseModel
{
public function getId(): int
{
}
//...
}
class DateTimeBaseModel extensa BaseModel
{
// Get set for created_at and updated_at
}
It works, until I need to chance a id collomn to bigint, and break all the inherance, I need to create new classes with return string for getId (of course, this was before PHP 8).
So, a fill years ago I needed to upgrade PHP from 5.6 to 7.2, reading the broken changings, I learn traits, I think, this is a better solution, for this example.
Like
Trait GetterSetterIdInteger
{
private int $id;
//Get set
}
class Model
{
use GetterSetterIdInteger;
use GetterSetterDateTime;
}
Thank you. There is a lesson about Traits in the second section. Traits don't really achieve the same thing as composition, yes it lets you reuse the code but that's about it. It shouldn't be used in place of composition in my opinion. It's better to pass down dependencies via constructor rather than include them within the same class using traits.
@@ProgramWithGio It's interesting, in your video game example I would definitly have gone with traits and interfaces.
Like CanAttackTrait (with implementation) and CanAttackInterface for the public contract.
This allows you to type hint in the functions with the interface while avoiding duplication of code. Even more in 8.1 with the intercetion type where we can say CanAttackInterface&CanMoveInterface.
And that makes more sense in my opinion than having something like an AttackerService.
What do you think? And why would you not call that composition ?
Thanks for the videos, this is very interesting even for someone who has been developing in PHP professionally for several years like me.
@@flo_111 Traits give you the feel of composition but essentially it just puts the code in the class, as far as testability goes its better to have dependencies injected in constructors to achieve true composition. Now it also depends on preference, Laravel likes to use traits to achieve composition & as long as its used properly I think its fine. I personally prefer dependencies injected in constructors. Interfaces + traits is not a bad option.
comming this far in this tutorial can i quickly learn codeignitor and start my journey of programmer or should i have to learn more.?
You should be able to get started with a framework.
Wow, I can't believe I've come this far! Master Gio, I have a question for you. I've watched all the lessons up to this point, and I've written every line of code you've written. I've completed all the exercises so far.
However, I'm having trouble memorizing everything with 100% efficiency. Some information is starting to slip my mind. Since your course is the best, I don't see the point in watching other PHP courses (although I could be mistaken). So, after finishing your course and learning the basics of Laravel, I think it will be time to start applying for PHP developer jobs. As the next step in my PHP/some framework learning, I believe working with the original documentation would be beneficial.
Could you give me some advice on how to properly work with documentation during my workflow? I mean, I don't think it's like reading Dostoevsky page by page, chapter after chapter (but I could be mistaken again). I think it would be a great idea for a new lesson: "How to effectively use PHP documentation to learn PHP the right way." 😄
That's awesome, good job. You shouldn't memorize anything, cause then it's expected to forget things. I also don't remember a lot of the things all the time. The key thing is to look it up when in need. Once you are exposed to the idea and have practiced and know what it is about, when you come across it 3-4 months later it will kick in and come back to you from memory.
As far as documentation, I wouldn't recommend just reading through all the documentation, instead you should read the pages of the things that you need for the project you are working on.
@@ProgramWithGio thank you!
So, car and engine is composition. car and passengers is aggregation? car can't live without engine but passengers can come and go?
Depends how you look at them but in a way yes. Think of aggregation as a container for associated objects.
Thanks for the video. It was preety amazing. So Composition and Dependency Injection meant to be the same, Am i right!
Thank you. Not really, dependency injection is a way to get composition.
Can you say dependency injection is the same as composition ? Or is there something else that sets them apart. Honestly never heard it being called composition.
And what about traits? Don't they go against code duplication also? Couldn't you add attack to a trait and let it be used by the monsters and the pets?
Composition is achieved by dependency injection. Of course you can instantiate the dependencies in constructor for example to achieve composition but dependency injection is better & we covered that earlier in this series.
About the traits I'll paste in the answer I gave to others:
You can use traits for code re-use, now does traits mean composition? That's debatable. In my opinion traits != composition because composition means class is composed of objects, when you use traits you don't have the reference to the object, it simply just pastes in the code in your main class. It is good for code re-use & allows for horizontal code re-use but I would not call it composition as tempting as it might sound.
the wolf of wall street scene gave you a subscriber lmao
😁. Thank you 🙌
I am here at video 80, 58 yet to go 🏃🏻♂🏃🏻♂
wohoo, awesome job so far
@@ProgramWithGio thank you for the awesome teaching 😊
Remember Composition Over Inheritance !
✅
So you gave us a problem with the game objects and no solution? not the best of ways to teach.
I showed 2 examples, not sure what you mean?
This is just a wrong use of methods like SalesTacCalculator. It should be trait, not a parent class.
Why **should** it be a trait? What's wrong with it being a class? Also note that this is just an example of inheritance vs composition lesson. Doing composition by injecting dependencies in constructor is the right way to do it instead of traits, makes writing tests easier.