0

我开始使用 oop 构建用户生成数据的站点。

我的课程中有不同类型的数据。我有从数据库中获取数据列表的函数,以及从这些列表中选择一项的函数。例如

function Get_Article($aid); //Gets an article
function Get_Users_Articles($uid); //Gets a multidemsional array of the users 
                                   //articles
function Get_Latest_Articles(); //Self explanatory by now
function Get_Local_Articles($zip); //Gets articles written by local users
function Get_Local_Users($zip); //Gets all local users

function Get_All_Article_Comments($aid); //Gets a multidimensional array of the 
                                     //comments written for an article
function Get_Article_Comment($cid); //Gets a single article comment

现在,我应该如何设置我的类来组织这些功能。我应该将它们全部放在同一个类中,还是应该将评论与文章分开,或者将检索单个文章/评论的函数与检索文章/评论列表的函数分开。稍后我可能会在网站上添加更多允许评论的内容,所以我正在考虑将所有评论功能与其他功能分开。此外,“本地”函数都使用执行数学运算的相同函数,所以我应该将它们组合在一起,或者只是使用继承......有什么建议吗???

在 oop 的主题上,我有一个用户类,看起来像...... private $user = array();

public function Get_User_Data($uid){
  //get user data from database
  return $this->user;
}

public function Set_User_Data($user_array){
  $this->user = $user_array;
}

public function Add_User(){
  //INSERT data from $this->user into the database 
}

现在有没有人看到这看起来有什么问题,主要是,我应该将 user_data 设置为 Add_User 函数的参数,而不是插入成员变量吗?

4

1 回答 1

6

首先,您需要了解,采用旧的过程式函数并将它们包装在对象中并不会使您的代码面向对象,这仅意味着您正在编写更复杂且丑陋的过程代码。

Secondly I would strongly recommend, in fact I cannot be too strenuous in my recommendations that you take some time to at least study the various PHP Frameworks that are out there. While you may never use any of them, I feel pretty safe in guaranteeing that studying any of them will give you a better grasp on object-oriented principles and good application design in general. In case you've never seen any before, the following should give a place to start:

  • Zend Framework
  • Symfony
  • CakePHP
  • Solar Framework

Additionally, if you've never heard of Martin Fowler or Patterns of Enterprise Application Architecture, I would strongly recommend that you try and pick up a copy. He literally wrote the book that has provided the basic patterns that are in use in EVERY popular web framework.

So much for my 'read the manual response' :-P

In your particular case I would start with a basic Active Record pattern to contain your database access logic and your domain logic. In this type of pattern, each database table (users, articles, comments) is represented by a discrete object. The basic Active Record class for Users would contain all of the functions to get a specific user or a list of users as well as the functions to insert, update, or delete a user. In addition, a User Active Record class would contain methods to load a user's articles and comments.

A shell User class might look something like this:

class User extends Active_Record {

public function find() {}

/**
    Single function performs inserts and updates for the object
**/
public function save() {}

public function delete() {}

public function getArticles() {}

public function getComments() {}
}
于 2009-08-27T03:26:03.640 回答