Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing a PHP class. The right way

Tags:

php

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?

like image 674
JohnB Avatar asked Nov 14 '22 14:11

JohnB


1 Answers

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.

like image 121
GSto Avatar answered Dec 17 '22 17:12

GSto