简体   繁体   中英

Designing a PHP class. The right way

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?

If you want to write it yourself from scratch, I'd start by making separate objects for each part of the blog, ie 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.

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 (eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM