tags:

views:

105

answers:

3

I am making a simple blog and I want to use a class for this. I am wondering what the best way of doing this may be. There are 3 methods so far that I can see. I made a class that got links, posts and comments. The problem with returning all of this data in a single array was that the data that only should have been echoed a single time was echoing as many times as the rows I was returning. Some people would say that this was correct and that I should have everything in a single array. I'm still not convinced this is the right way to go.

I'd really appreciate some input as to how some of you guys might design a class to handle these 3 things. The only thing I know I need for the class so far is a link to the database. What other members I should use are unclear to me. Again, I'd like to gain a perspective here on how it should be done.

Thanks, John


OK, so if I had say 3 methods like this:

var $db;

function GetPosts() {
//posts
}

function GetComments() {
// blog comments
}

function GetLinks() {
// links for the blog
}

What might be some of the members you guys would use?

Also, I'm wondering if I should have a method that calls other methods. Just one public method and then make all of these other methods private. The public method would call the private methods and return their values. Is this advisable?

A: 

CakePHP has a tutorial on creating a blog using the widely adopted CakePHP framework. If you're interested in learning a new framework whilst programming your blog, I would recommend doing that.

If you'd like to hand-code everything though I would suggest that you create classes for handling loading and saving (persisting) your entites (links, posts comments) or use a persistence framework (e.g. Doctrine ORM for PHP).

The problem is though that you can implement the functionality you've described in numerous different ways; there's no universal answer. If you submit some code it's easier to give specific advice.

Gergely Orosz
Hi Gergely Orosz, thanks for the response. I have tried a couple of different frameworks already but I don't much care for them. To me anyway, it takes the fun out of doing it myself. I know that there are a lot of helper functions and all but I have met other "programmers" that have no idea what goes on behind that framework. Don't get me wrong, I like frameworks but I like the inter-workings a little more. I will add a few things to my class above and maybe you can give me some pointers. Thanks again!
JohnB
+1  A: 

If you want to write it yourself from scratch, I'd start by making separate objects for each part of the blog, i.e. a post object, a comments object, a link object.

Then you could think about how those objects will interact with each other (for example, a post object may contain an array of comment objects) and how they will handle the basic CRUD (create, read(get),update,delete) operations.

GSto
Hi GSto, thanks for the reply. I agree with you about having a seperate class for each part of the blog. I have heard that doing it like this makes it harder to keep the abstraction intact. I'm still not sure about this though. I'm going to add a few things to the methods above and maybe you can let me know what you would do to change it.
JohnB
how to thing doing it this way would hurt the abstraction? I believe that it would be easier to maintain this way, instead of having one blog class that handles everything.
GSto
A: 

I don't know the details of the task you want to achieve, but think about one factory class Blog, that has GetPostManager() method, for example. PostManager can create, edit, update posts and select them as well. So, $Blog->GetPostManager()->GetPosts($datefrom, $dateto) will return PostCollection class, that implements Countable and Iterator interfaces to browse for posts. PostCollection is an array, containing PostItem class. PostItem class is a simple class, that can map to database it's properties.

FractalizeR
Thanks FractalizeR. The factory pattern sounds good. So, would you say that it would be wrong to create a class with these 3 methods? I know that there are more than 100 ways to do things but I guess I'm looking for the "best" way. Not necessarily the easiest. I figure that it should ultimately be able to scale if I ever wanted to do that.
JohnB
No, class with three methods is good... But until you extend your system. Should I want to add more features, your one class will quickly become overcrowded by more and more methods. So, I think it's better to create some structure at the beginning. Especially, if it is not very complex.
FractalizeR