OO Design Question

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

OO Design Question

Post by Sekka »

I need to consult an OO boffin of sorts. I am having trouble getting my head around part of the OO design concept.

I have made a class that will control a blog post. It will load it, alter it, update it, etc, basically control all the data relating to that blog post only.

Now, I want to display on my website a list of recent blog articles. Now, the best way I see of doing this is just to query the DB for the latest posts and display the data. But, this makes my class relatively useless as I am bypassing it completely.

I then started forward thinking to when I would actually use my class, and the only time I think I would use it is when I am loading a specific blog post page or just loading data related to 1 blog post, or when I am administering it through the CMS.

Does this sound right or have I deviated from the OO path in my system?
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Re: OO Design Question

Post by aliasxneo »

Use a static function.

Code: Select all

blogClass:getRecentPosts();
That's the way I always do it. Since most of my objects take an ID in the constructor which it uses to wrap around a specific entry in the database, using static functions was pretty much my only option for handling more than one object.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OO Design Question

Post by Christopher »

If you have a class that defines one blog post, then just create another class that finds and returns a list of blog post objects.
(#10850)
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: OO Design Question

Post by Sekka »

Thanks for the replies.

I've been thinking about this since I left work this afternoon.

What I'm probably going to do is create a base class that holds the data for a specific post, and the methods that format that data in a specific way. This will essentially be a data container that is simply passed raw data to store and output on request.

I can then build around it other classes that use and extend it. For example,

* A method to get the recent posts can query the posts and create an array of data container classes to be used else where.
* A class can extend the data container for admin purposes and manipulate and store the data.

This will give me a nice degree of separation I think and allow me to accomplish what I need.

This sound like OO implementation or am I completely off the reservation?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OO Design Question

Post by Christopher »

I would not create a base class until you know you need it. Have you thought about Row Data Gateway or Table Data Gateway for this?
(#10850)
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: OO Design Question

Post by Sekka »

I just Googled those.

My core classes essentially are those at the moment. They have no display logic or anything, they simply load data, allow manipulation through the API, and then update the data, etc.

I just need to rethink how I have things laid out. I definitely need to separate the admin methods from my class anyway as they are rarely needed.
User avatar
hongster
Forum Newbie
Posts: 6
Joined: Fri Mar 30, 2007 12:54 am

Re: OO Design Question

Post by hongster »

Sekka wrote:... Now, I want to display on my website a list of recent blog articles. Now, the best way I see of doing this is just to query the DB for the latest posts and display the data. But, this makes my class relatively useless as I am bypassing it completely. ...
Yes, you can bypass the class completely but the is procedural programming. One of the features of OO programming is encapsulation. The retrieval of data from DB can be placed in a class. When the method of retrieving data is changed (e.g. different database, reading from text file, ...), changes to your code will be limited to 1 class. It give you flexibility.

A class is useful for grouping related functions. These functions may have a tight coupling with each other. Data that are shared among these functions will become the class's members.

These are just some of my thoughts, not sure if it helps you :oops:
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: OO Design Question

Post by Kieran Huggins »

Embed the DB calls (Create, Read, Update, Delete) in the Post class. You can then use it to control all the data for each blog post.

If you want to get a series of posts, you can fetch the IDs of matching posts and instantiate a Post object for each one.

There ways to make this more efficient, but it's a damn fine place to start when earning OOP. Refactor when you feel the pinch, keep it simple until then ;-)

We're here to help!
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: OO Design Question

Post by Sekka »

Thanks for the comments guys.

I ended up writing like an 'interface' class if you will with methods for loading multiple blog posts, then those methods returned an array of objects.

I realised I was thinking too much like a pre-optimiser and I needed to start focusing on OO, which I have done.

Thanks!
Post Reply