Using the composition pattern to generate, clean, well organised, flexible object orientated PHP code is a popular method with todays web programmers. Below is an example of two possible approaches to store a reference of one object to another - a form of composition.
To inspire some interaction we will give the best comment to this post one fully spiderable site wide blogroll link - at our discretion of course, the target site should be of value to our target audience of web developers, network admins and seos.
Background using Patterns for PHP5 Development
Recently I have been reading my way through Apress’ PHP Objects and Patterns edition 1, and it is a fantastic read. Very interesting especially if you are getting your hands wet with implementing patterns in PHP 5.
I have had the privilege of working with some very clever object oriented php programmers for the last 5 months, my programming has come a long way and I have really never enjoyed PHP programming so much.
I shudder to think of the days trawling through OS Commerce, or even wrestling with cross browser specific css. Its been a great opportunity to focus on what I enjoy most - object oriented programming.
With the object orientation of PHP 5 and namespacing in Javascript coding for the web has never been so enjoyable.
In the Spirit of Debate
One FREE Link
This blog sorely lacks debate, so we would like to hear comments or any remarks on the below. Why bother? - The most useful comment will receive a non-no followed link to their blog in our blog roll.
The blog/site must be of a related subject and useful to readers however. The link will stay for at least one month, we are pretty slack here so its likely to sit there longer.
Composition
I had to change a concrete abstract class and some of the leaf classes it inherited from. Although its not a pure example of composition in terms of the composition pattern, I needed to store another object in an instance variable.
Think of object Alpha as a product. Object Beta as a company. Alpha has a Beta company that it belongs to.
Alpha would be stored in the database frequently, it would also be passed to the view frequently. Say 50% of the time you are inserting Alpha into your db, 50% of the time you are passing Alpha to the view.
When passing Alpha to the view you need to retrieve the company Beta for information about the product Alpha. Storing the Beta object in an instance variable of Alpha would be handy in this case.
When passing Alpha to the model to insert in the db all you need is the id for the company Beta. Storing simply the id for Beta would be handy in this case.
The Crux of the Matter
Would you store the id or the object of Beta in an instance variable of Alpha?
Storing simply the id is lightweight, but if the ids for the Beta objects in the db change, then your code is coupled to that change.
Storing Beta objects in an instance variable of Alpha means one more request to the db every time you create an Alpha object, 50% of the time this will be unnecessary as you will be inserting the Alpha object and only the id for the Beta object is required for insertion.
Does that make sense? Thoughts/feedback welcome - the best comment earns a sitewide inbound.
Related posts:
No tags for this post.
Leave a Reply