我是用对象还是 OOP 编写过程代码?
所以基本上我正在从过程编码飞跃到面向对象编程。 我正在尝试实现 OOP 的原则,但我有一种挥之不去的感觉,我实际上只是用对象编写过程风格。
假设我有一个管道/椅子/打印机/任何东西的列表,它们都作为产品列在我的单表数据库中。我需要构建一个 Web 应用程序,根据其类型显示整个列表和项目,重点是“正确”使用 OOP 及其范例。
这样做有什么问题吗:
类展示 { 公共函数 showALL(){ $prep = "从我的产品中选择 *"; $q = $this->db->;准备($准备); $q->execute(); while ($row = $q->fetch()) { echo "bla bla bla 一些排列的显示".$row['something'] } }
然后
$sth = new show();
$sth->showAll();
我还会实现更具体的显示方法,例如:
showSpecificProduct($id)->(当用户点击其中一个链接时,$id 将通过 $_GET 传递,我们将拥有单独的product.php 文件,该文件将基本上只包含
include('show.class.php');
$sth = new show();
$sth->showSpecificProduct($id);
showSpecificProduct() 会同时执行选择查询和输出 html 以进行显示。
所以简而言之,我是在做这件事还是只是用类和对象进行程序编码还有任何想法/提示。如果我做错了,如何解决它?
So basically I'm making a leap from procedural coding to OOP.
I'm trying to implement the principles of OOP but I have a nagging feeling I'm actually just writing procedural style with Objects.
So say I have a list of pipes/chairs/printers/whatever, they are all all listed as products in my single table database. I need to build a webapp that displays the whole list and items depending on their type, emphasis is on 'correct' use of OOP and its paradigm.
Is there anything wrong about just doing it like:
CLass Show { public function showALL(){ $prep = "SELECT * FROM myProducts"; $q = $this->db-> prepare($prep); $q->execute(); while ($row = $q->fetch()) { echo "bla bla bla some arranged display".$row['something'] } }
and then simply
$sth = new show();
$sth->showAll();
I would also implement more specific display methods like:
showSpecificProduct($id)->($id would be passed trough $_GET when user say clicks on one of the links and we would have seperate product.php file that would basically just contain
include('show.class.php');
$sth = new show();
$sth->showSpecificProduct($id);
showSpecificProduct() would be doing both select query and outputing html for display.
So to cut it short, am I going about it allright or I'm just doing procedural coding with classes and objects. Also any ideas/hints etc. on resolving it if I'm doing it wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除了@Phil 和@Drew 描述的模型实践之外,我还敦促您将业务层、数据层和视图层分开。
我已经包含了一个非常简单的版本,需要在您的实现中对其进行扩展,但其想法是使您的 Db 选择与输出分开,并几乎在控制器中将两者“连接”在一起。
As well as the model practices described by @Phil and @Drew, I would urge you to separate your business, data and view layers.
I've included a very simple version which will need to be expanded upon in your implementation, but the idea is to keep your Db selects separate from your output and almost "joining" the two together in the controller.
更好的选择是实施存储库模式。一个示例接口可能是
您然后创建该接口的具体实现
直接从方法或函数
echo
通常是一个坏主意。让您的方法返回适当的对象/数组/任何内容并使用这些结果。A better fit would be to implement a repository pattern. An example interface might be
You would then create a concrete implementation of this interface
It's generally a bad idea to
echo
directly from a method or function. Have your methods return the appropriate objects / arrays / whatever and consume those results.您上面描述的场景似乎是 MVC 的良好候选者。
在您的情况下,我将创建一个严格用于访问数据的类(选择产品类别或特定产品),然后让另一个文件(您的视图)获取输出并显示它。
它可能看起来像这样:
然后您可以在其他地方执行以下操作:
MVC 的基本原则是您拥有模型类,这些模型类只是表示来自某些数据源(例如数据库)的数据的对象。您可能有一个映射器,用于将数据库中的数据映射到数据对象或从数据对象中映射数据。然后控制器将从模型类中获取数据,并将信息发送到视图,在视图中处理实际的呈现。在控制器中拥有视图逻辑(html/javascript)是不可取的,并且直接与控制器中的数据进行交互是相同的。
The scenario you are describing above seems like a good candidate for MVC.
In your case, I would create a class strictly for accessing the data (doing selects of product categories or specific products) and then have a different file (your view) take the output and display it.
It could look something like this:
Then somewhere else you can do:
The basic principle of MVC is that you have model classes that are simply objects representing data from some data source (e.g. database). You might have a mapper that maps data from the database to and from your data objects. The controller would then fetch the data from your model classes, and send the information to the view, where the actual presentation is handled. Having view logic (html/javascript) in controllers is not desirable, and interacting directly with your data from the controller is the same.
首先,您需要研究类自动加载。这样,您不必包含您使用的每个类,您只需使用它,自动加载器就会找到正确的文件来为您包含。
http://php.net/manual/en/language.oop5.autoload.php
每个类都应该有一个单一的职责。您不会有一个连接到数据库并更改某些用户数据的类。相反,您将拥有一个数据库类,您将其传递给用户类,并且用户类将使用数据库类来访问数据库。每个职能也应该有单一的职责。你永远不应该在函数名称中添加“and”。
您不希望一个对象知道另一对象的属性。这会导致在一个类中进行更改迫使您在另一类中进行更改,并且最终会变得很难进行更改。属性应该供对象内部使用。
在开始编写类之前,您应该首先考虑您希望如何使用它(请参阅测试驱动开发)。您希望代码在使用时看起来如何?
既然您知道如何使用它,那么以正确的方式对其进行编码就容易得多。
有机会时研究敏捷原则。
first, you will want to look into class autoloading. This way you do not have to include each class you use, you just use it and the autoloader will find the right file to include for you.
http://php.net/manual/en/language.oop5.autoload.php
each class should have a single responsibility. you wouldn't have a single class that connects to the database, and changes some user data. instead you would have a database class that you would pass into the user class, and the user class would use the database class to access the database. each function should also have a single responsibility. you should never have an urge to put an "and" in a function name.
You wouldn't want one object to be aware of the properties of another object. this would cause making changes in one class to force you to make changes in another and it eventually gets difficult to make changes. properties should be for internal use by the object.
before you start writing a class, you should first think about how you would want to be able to use it (see test driven development). How would you want the code to look while using it?
Now that you know how you want to be able to use it, it's much easier to code it the right way.
research agile principles when you get a chance.
一条经验法则是类名通常应该是名词,因为 OOP 是关于拥有与真实概念对象相对应的软件对象。类成员函数通常是动词,即可以对对象执行的操作。
在您的示例中, show 是一个奇怪的类名。更典型的方法是使用一个名为 ProductViewer 的类,并带有一个名为 show() 或 list() 的成员函数。此外,您还可以使用子类来获取专门的功能,例如特定产品类型的自定义视图。
One rule of thumb is that class names should usually be nouns, because OOP is about having software objects that correspond to real conceptual objects. Class member functions are usually the verbs, that is, the actions you can do with an object.
In your example, show is a strange class name. A more typical way to do it would be to have a class called something like ProductViewer with a member function called show() or list(). Also, you could use subclasses as a way to get specialized capabilities such as custom views for particular product types.