15.
Object Composition FTW!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
25 Comments
Thank you for this series of tutorials. This has done wonders for my understanding of OO PHP. Truly helpful!
Wooo! Cheers Jeremy Carlson :)
Brilliant series, thank you so much guys! <3
Hey Aristotele,
Thank you for you feedback! It made our day! :)
Cheers!
Thank you for the wonderful and informative series of tutorials. Coming from old-skool php land this has brought me up to speed in a hurry. Now to go code something!
Hey Brian!
We are glad to hear that you find useful our tutorials, we make them with passion ;)
Cheers!
I'm kind of lost:
Can you explain why this example is "Object Composition" instead of "Object Aggregation" ?
As I see Composition is described as:
"In composition, parent entity owns entity.
and Aggregation as:
"In Aggregation, parent Has-A relationship with entity"
So in that case isn't "Composition" called when the storage object is created inside the LoggableShipStorage constructor and "Aggregation" when is past from the outside as the example?
Hey @Hristo-I!
Sorry for my very slow reply! To be totally honest, I'm not sure :). Coming from the PHP world, object aggregation isn't a term that I'm even familiar with. This may mean it's less common in PHP... or may just be a product of the fact that php devs have called this "object composition" and that has "caught on" much more than object aggregation. From some quick searching, these concepts are quite similar. The best I can say is this:
So the key thing is that we're showing a strategy for solving a specific problem. And, in PHP, we tend to call this solution "object composition". To the specifics of composition vs aggregation, I will fully admit that I don't know the difference. However, about this:
I don't know about the correct naming, but the second part - where it is passed from the outside - is a much more common thing to do. And yes, this might technically be "aggregation" as aggregation implies a weaker relationship (i.e. a relationship controlled from the outside).
I hope this helps... at least to know that the difference is subtle and you are probably thinking about this already more deeply than most devs are :).
Cheers!
Hi, what is the difference with dependency injection? That dependencies are required to work, while composition is just a wrapper?
Hey Naschkatze,
Composition and dependency injection could be seen as the same thing because you achieve the same goal, which is composing your classes. For example, a big method could be split into two different classes and just inject (adding a property field), let's say, classB into classA, so classA would make a call to some method on classB. Does it make sense to you?
Cheers!
Finally done with OOP Full Course.
It's been a really nice experience full of fun.
Great examples, great way to explain the topics, but in some cases a few exercises were a little bit confusing!.
I'll definitely keep studying the rest of the courses!!!.
Hey Roi!
Congrats! You did a huge job finishing all the episodes in OOP :) And we're happy to hear it was useful for you! If something was confusing - feel free to leave a comment below the related chapter. Fairly speaking, yeah... there might be some tricky challenges, that's because we just wanted to make things not that easy at the fist sight ;)
Good luck with further learning!
Cheers!
absolutely brilliant!. thanks guys!
Hi ! How come the log says "just fectched 4 ships", whereas there are 5 ships ? And why doesn't Slave I (Bounty Hunter) have any Jedi Factor ? Thank you in advance !
Hey @Ana
Nice question! If you look at ShipLoader::getShips() you will notice that it hardcodes an extra Ship into the array, so the Logger will never know about it. I hope it anwsers to your question :)
Cheers!
So in this example our class LoggableShipStorage is actually implements a Proxy pattern?
I wish someday your Design Patterns course will be completed :)
Hey Ivan!
Actually, yes! There are many different use-cases for the proxy pattern, but this is certainly one of them: the LoggableShipStorage is effectively a proxy for whatever other ship storage we pass into it. In fact, it fits the "smart reference" proxy pattern that Marco (core Doctrine contributor) talks about in his presentation here: http://ocramius.github.io/p...
Cheers!
In this case, use the composition, but if I have only one handler, can I use the inheritance?
Totally :). I think using inheritance is a bit more common when people develop their *own* applications, as it's a bit more straightforward, and you probably don't need the flexibility that composition gives you. This happens a lot actually: if you're building a re-usable library, then you often need to code things "more correct" than you should need to in your own code :).
Cheers!
Typo in challenge one:
But here's the challenge: the existing PlanetRenderer returns a div **aroudn** the plane.
Laaaame :). Thanks a lot for reporting that - fixed at https://github.com/knpunive... I wanted to give you a GitHub shout-out, but couldn't find your user!
Cheers!
You couldn't as I wasn't registered until now ;)
And another remark: at 2:30 you are saying return (as it's written in the script), but fetchSingleShipData does not return anything... :)
When I mess up (i.e. by forgetting the return) we keep the code and script correct :). But we'll add a note about the missing return.
Thanks!
"Houston: no signs of life"
Start the conversation!