用于演示的 php 类结构
我需要经验丰富的开发人员对此的建议。让我直接进入正题。
我有用户发布文章,我需要显示文章列表并显示发布文章的用户。
在我当前的预处理方法中,它是这样的:
$result = mysql_getresult("SELECT users.*, articles.*
FROM users JOIN articles ON users.id = articles.user_id
WHERE 1");
while($row = mysql_getrow($result)) {
display_username($row);
display_articletitle($row);
}
现在我想做 OO 方法 OO
class User {
protected $data;
public function loadUserData($id) { $this->data = mysql_readstuff() }
public function displayUserName() { echo $this->data['name'] }
}
class Article {
protected $data;
public function loadArticleData($id) { $this->data = mysql_readstuff() }
public function displayArticleTitle() { echo $this->data['title'] }
}
问题:
1.) 这些类有自己的方法来加载自己的数据(以及许多其他方法),因此快速 mysql 查询可以获取所有数据使用 JOIN 立即获取数据在这里并不起作用。加载自己的数据对于许多其他方法(例如身份验证)来说非常有用。
2.) 我想分离演示文稿(使用 savant3 templater),那么我应该将显示方法放在哪里?我需要显示很多东西(作业、评论等),并且我认为在模板器中为它们中的每一个创建一个方法不是一个好主意。
这个例子是用户和文章,但我将有用户+评论,用户+工作+要求。
我想过将显示方法设为静态并让它们接受参数,但它与过程方法没有什么不同。或者创建一个 setData($row) 方法,将 $data 设置为其获得的参数。
也许这个问题有一个通用的解决方案。
任何建议表示赞赏。
谢谢你, 斯卡切尔
I need experienced developers advice on this. Let me get straight into it.
I have users posting articles and i need to show a list of articles and display the users that posted them.
In my curent precedural method its something like this:
$result = mysql_getresult("SELECT users.*, articles.*
FROM users JOIN articles ON users.id = articles.user_id
WHERE 1");
while($row = mysql_getrow($result)) {
display_username($row);
display_articletitle($row);
}
Now i want to do it OO method
class User {
protected $data;
public function loadUserData($id) { $this->data = mysql_readstuff() }
public function displayUserName() { echo $this->data['name'] }
}
class Article {
protected $data;
public function loadArticleData($id) { $this->data = mysql_readstuff() }
public function displayArticleTitle() { echo $this->data['title'] }
}
problems with OO:
1.) These classes has their own methods for loading their own data (and many other methods) so the fast mysql query for getting all the data at once with the JOIN is not really working here. Loading their own data is great for many other methods (eg authentication).
2.) I want to separate presentation (using savant3 templater) so where should i put the display methods? I will need to display a many many things (jobs, comments, etc) and i dont think makeing a method for each of them in the templater is a good idea.
The example is with users and articles, but i will have user + comment, user + job + requirements.
I thought of making the display methods static and let them take a parameter, but then its no different then the procedural method. Or make a setData($row) method that will set the $data to be the parameterthat it got.
Probably there is a common solution for this problem.
Any advice is appreciated.
Thank you,
Skaccer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Article 类不应具有加载方法或显示方法。
我的建议是创建一个 表网关:
现在
$articles
包含文章对象的数组。您可以在视图中对其进行迭代并显示文章。An Article class should not have a load method, or a display method.
My advice would be to create a table gateway:
Now
$articles
contains an array of Article Objects. You can iterate over it in your view and display the articles.正如 @galen 所说,
不要让类方法打印出数据,让它们返回数据数组,然后根据需要查看它们来处理它们:
我在开发应用程序时经历过这种情况。我可以使用我的方法加载 Android 应用程序数据以及网站应用程序
Exactly as @galen said,
Dont let your class methods print out your data, let them return an array of the data, then you handle them as you need to view them:
I experiences this when developing my application. i can use my methods to load an android app data as well as a website application