开发者

Help with choosing structure of code to make NewsHandler class

Task is to add News on our site.

I'd like to make a php class for this task. This class should be able to do such things:

  1. Add news
  2. Edit news
  3. Remove news
  4. Display exact news
  5. Get list of 开发者_运维百科news

News can be of 3 types: local news (news of organizations registered on site), global news (news that add administration of the site), and stock (hot offers from organizations)

Help to choose correct class structure for such a task. I thought, it can be a static class, for example NewsHandler that has static methods:

  • add ($type_news, $title, $announcement, $content, $picture)

    (method will return the id of news)

  • edit ($id, $new_title, $new_announcement, $new_content, $new_picture)
  • remove ($id)
  • display ($id)
  • getAll ($type_news) (method will return the list of id's of news)

This is very trivial approach. Is this wrong? May be I should do bigger class structure - with inheritance? For example:

  • static class NewsHandler
  • class News (that produces News instances)
  • classes LocalNews, GlobalNews, Stock (extending from News)

If so, I don't know which methods each of them should have


I would say that, in quite simple terms, this would be an okay setup. However, as pointed out in the comments, you probably need more than these classes. OOP is very much top-down thinking, where you first have to oversee what all the stuff is you're going to do. For instance, the news probably has to be stored in a database. So: database class. And you might want to use some sort of validation, eg. validation class. Then there's rendering the stuff. Throw in a couple of classes for those, for templating, formatting etc.
As you can tell, your news classes are going to use all these other classes. So, while your setup looks okay from the news point of view, there's bound to be a lot more around it. And that's why there's no clear and simple answer to your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜