我应该如何设置我的课程?
我开始使用 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 主题上,我有一个用户类,看起来像...... 私有$用户=数组();
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 函数的参数,而不是插入成员变量吗?
I'm starting to work with oop to build a site of user generated data.
There are different types of data coming from my classes.
I have functions that get a list of data from the database, and functions to just select one item from those lists.
For example
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
Now, how should I set up my classes to organize these functions.
Should I just put them all in the same class, or should I seperate the comments from the articles, or maybe seperate the functions that retrieve a single article/comment from those that retrieve a list of articles/comments. I might add more things to the site later that allow for comments, so I was thinking of just seperating all the comment functions from the others. Also, the "local" functions all use the same function that performs the math, so should I group those together, or maybe just use inheritance... any suggestion???
While on the subject of oop, I have a user class that looks like...
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
}
Now does anybody see anything wrong with the way this looks, mostly, should I set the user_data as a parameter for the Add_User function, instead of inserting the member variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要了解,采用旧的过程样式函数并将它们包装在对象内并不会使您的代码成为面向对象的,它只是意味着您正在编写更复杂且极其丑陋的过程代码。
其次,我强烈建议您至少花一些时间研究现有的各种 PHP 框架,事实上,我的建议不能太费力。虽然您可能永远不会使用它们中的任何一个,但我确信学习它们中的任何一个都会让您更好地掌握面向对象的原则和良好的应用程序设计。如果您以前从未见过,可以从以下内容开始:
此外,如果您从未听说过 Martin Fowler 或企业应用程序架构模式,我强烈建议您尝试一下并拿起一份副本。他确实写了这本书,提供了每个流行的 Web 框架中使用的基本模式。
我的“阅读手动响应”就到此为止了:-P
在您的特定情况下,我将从基本的 Active Record 模式开始,以包含您的数据库访问逻辑和域逻辑。在这种类型的模式中,每个数据库表(用户、文章、评论)都由离散对象表示。用户的基本 Active Record 类将包含获取特定用户或用户列表的所有函数以及插入、更新或删除用户的函数。此外,用户活动记录类将包含加载用户的文章和评论的方法。
shell User 类可能看起来像这样:
First of all you need to understand that taking your old procedural style functions and wrapping them inside of objects doesn't make your code object-oriented, it just means that you're writing more complicated and horribly ugly procedural code.
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:
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: