OOP在小型开源项目中的应用
所以我目前对类的理解是: Singleton 用于只能实例化一次的类。 静态用于未实例化但仅存在的类。 常规的?对于可以反复实例化的类。
所以我正在做一个小型的开源项目,至于与用户打交道,我想到了如何处理它,例如: 创建用户 - 我可以实例化一个用户对象,然后调用它的创建方法。或者我可以有一个单例,以便用户对象始终存在并在其上调用创建?
我只是认为为每个用户相关操作创建一个对象(例如更新用户凭据)似乎有点草率,我是否想实例化另一个用户对象,然后对其调用方法更新?
只是对如何实际应用 OOP 以及最佳方法感到困惑。
感谢你们提供的任何/所有帮助。
So my current understanding of classes are:
Singleton for a class that will only ever be instantiated once.
Static for a class that doesn't get instantiated but just exists.
Regular? For a class that can get instantiated over and over.
So I'm doing a small open source project and as for dealing with users, I thought of how I could deal with it, for example:
Creating a user - I could instantiate a users object and then call a method create on it. Or I could have a singleton so the users object always exists and call create on that?
I just think it seems sort of sloppy to create an object for each user related action, like updating a users credentials, would I want to instantiate another user object and then call a method update on it?
Just confused about how to actually apply OOP, and the best way to do.
Thanks for any/all help you guys can provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使这是一个小项目,我也建议您查看可用的 PHP 框架。 CodeIgniter 占用空间小,并且支持快速部署。
对于这种情况,如果我们忽略框架的可能用法,我会使用一个如下所示的 User 类:
Even if it's a small project I'd recommend looking at the available PHP frameworks. CodeIgniter leaves a small footprint and embraces fast deployment.
For this case, if we leave out the possible usage of frameworks I'd go with a User class that would look something like this: