PHP OOP Best Practices Question
I have been playing around with OOP for a little bit, trying to wrap my arms around the concepts and I have run into a few different variations of the same problem. It seems there are multiple ways of handling it and I curious what the best option is?
I have two classes:
- Document Information
- Date Formatting
Neither of these classes are an extension of the other.
The Document Information class contains the date the document was originally created in a unix timestamp. I want to format it as a readable date (i.e. 4/19/2010). My "Date Formatting" class is already set up to format the dates the way that I like.
The way I understand OOP to work is that rather than formatting each date throughout the application individually (by placing date("D F d Y", $timestamp) wherever a date is displayed). There would be a class that contains the appropriate method and the class would instantiated as necessary. That way, if the developer decides they don't like the "D F d Y" formatting, they could change it in one place and would apply throughout the application. Please correct me if I am off base.
Is there a good way for me to access the Date Formatting class within my Document Information class so I can format my date?
Would I instantiate a new Date Formatting object within the Document Information Class?
Or, should I create a Getter method in the Document Information class that will pass it to the Date Formatting class and back again?
I have been using the second option but it can get tedious quickly creating all those getter and setter methods. I really curious to learn how others are working through this or whether I have "programmed myself into an OOP corner"...