对于日常网络开发使用的简单对象有什么想法吗?

发布于 2024-09-01 11:23:10 字数 366 浏览 2 评论 0原文

天哪-我知道这是一个主观问题,所以可能会被启动/锁定,但无论如何我都会尝试,因为我不知道还能问哪里(请随时向我指出一个更好的地方来问这个问题!)

我只是用 PHP 来研究 oop,但我仍然没有使用框架或任何东西。

我想创建几个简单的小对象,可以在我自己的网站中使用它们,以便更好地了解它们。

谁能推荐一个列表或资源,让我说出人们会在基本网站中使用的 10 个日常对象?

我之所以问这个问题,是因为我自己有点困惑。例如,我正在考虑一个“数据库连接”对象,但后来我只是认为这只是一个函数,而不是真正的“对象”?

所以问题是:

基本 PHP 网站(不包括“购物车”类型网站)中使用的对象的一些示例有哪些?

谢谢!

Dang-I know this is a subjective question so will probably get booted off/locked, but I'll try anyway, because I don't know where else to ask (feel free to point me to a better place to ask this!)

I'm just wrapping my head around oop with PHP, but I'm still not using frameworks or anything.

I'd like to create several small simple objects that I could use in my own websites to better get a feel for them.

Can anyone recommend a list or a resource that could point me to say 10 day-to-day objects that people would use in basic websites?

The reason I'm asking is because I'm confusing myself a bit. For example, I was thinking of a "database connection" object, but then I'm just thinking that is just a function, and not really an "object"??

So the question is:

What are some examples of objects used in basic PHP websites (not including "shopping cart" type websites)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海夜未眠 2024-09-08 11:23:10

以下是您可能拥有的一些基本的可重用对象:

  • 会话(由 cookie 标识,存储在服务器端)
  • 用户(用户名、密码等)
  • DBConnection(是的,这可以是一个对象)
  • 注释(允许用户对事物发表评论

)听起来你想开始构建自己的 Web 框架,这是一种不错的学习方式。但不要重新发明轮子。对于生产站点,您可能最好使用现有框架。

Here's a few basic reusable objects you might have:

  • Session (identified by a cookie, stored server side)
  • User (username, password, etc.)
  • DBConnection (yes, this can be an object)
  • Comment (allow users to comment on things)

It sounds like you want to start to build your own web framework, which is a decent way to learn. Don't reinvent the wheel though. For a production site, you're probably better off using an existing framework.

月寒剑心 2024-09-08 11:23:10

既然您说过您不想再次粘合 HTML 和 CSS,那么您就不会尝试以下操作:

  • 创建一个 WebForm 类。此类是表单元素的容器。它具有添加和删除表单元素的方法。它有一个 getHTML() 方法,用于编写表单以便用户可以输入数据。进行 POST 时也是相同的对象。它有一个方法来验证用户的输入;它将验证委托给每个表单元素,然后进行某种全局验证。它有一个处理表单的处理方法。它是最终的,并检查验证是否已通过。如果通过,它会调用一个抽象的受保护方法,该方法实际上执行特定于表单的处理(例如,将行插入数据库)。表单可以存储在存储在会话中,也可以每次都重新构建(如果存储在会话中,则更容易制作多页表单)。
  • 创建一个 BaseFormElement,然后创建几个子类,如 EmailElement、PhoneElement 等。它们还有一个 getHTML() 方法,由 WebForm::getHTML() 调用并打印特定元素。它们有一个由 WebForm::validate() 调用的 validate() 方法和一个返回该元素经过正确验证和处理的数据的 getData() 方法。

这些只是一些想法。有些事情可能没有意义:p

Since you said you don't want to glue HTML and CSS again, you don't try this:

  • Create a WebForm class. This class is a container of form elements. It has methods to add and remove form elements. It has a getHTML() method that writes the form so that the user can input data. The same object is when a POST is made. It has a method to validate the input of the user; it delegates the validation to every form element and then does some kind of global validation. It has a process method that processes the form. It is final and checks whether validation has passed. If it passed it calls an abstract protected method that actually does the form-specific processing (e.g. insert rows into the DB). The form may be stored in the stored in session, or it may be re-built everytime (if it is stored in the session, it's easier to make multi-page forms).
  • Create a BaseFormElement and then several child classes like EmailElement, PhoneElement etc. These have also a getHTML() method that is called by WebForm::getHTML() and that prints the specific element. They have a validate() method that is called by WebForm::validate() and a getData() method that returns the properly validated and processed data of that element.

These are just some ideas. Some things may not make sense :p

第七度阳光i 2024-09-08 11:23:10

我想说数据库访问将是第一个最有可能的对象 - 将最常见的 SQL 请求封装到一个类中。如果您使它们足够抽象,您可以将它们用于各种数据访问情况。

考虑类设计/使用的方法是考虑类的责任。您应该能够用简短的句子(比这更短...)描述类的用途,即对于数据库访问对象,您可能会说:

“为常见数据访问任务提供API”

如果您的数据访问类中的任何方法做一些除此之外的事情,然后你就知道它们属于其他地方。

I'd say database access would be the first most likely object - encapsulate your most common SQL requests into one class. If you make them abstract enough, you can use them for a wide variety of data access situations.

The way to think about class design/usage is to think of the class responsibility. You should be able to describe the class purpose in a short sentence (shorter than this...) i.e for database access object, you might say:

"provides API for common data access tasks"

If any of the methods in your data access class do something other than that, then you know they belong somewhere else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文