php 中类的实例应该是什么?
在 PHP 5.2.x、mySQL 5.x 中,
我遇到了一个问题,就是什么应该是 php 中的类的实例,什么不应该是,因为一旦页面呈现,它们就不会持久。
假设我有一个评论列表。对我来说,每个评论都是它自己的对象是有意义的,因为我可以对它们调用操作,并且它们拥有属性。如果我用另一种语言(具有持久状态并且可以交互的语言)执行此操作,我会这样做。
但这似乎很浪费,因为要做到这一点,我有一个调用 new() 的循环,这可能意味着我需要访问每个实例的数据库(也很糟糕)。
但也许我错过了一些东西。
我对类和对象的看法似乎与 PHP 不同。什么时候应该是类实例,什么时候不是?
In PHP 5.2.x, mySQL 5.x
Im having a bit of an issue wrapping my head around what should and should not be an instance of a class in php because they are not persistent once the page is rendered.
Say I have a list of comments. To me, it would make sense that every comment be its own object because I can call actions on them, and they hold properties. If I was doing this in another language (one that has persistent state and can be interacted with), I would do it that way.
But it seems wasteful because to do that I have a loop that is calling new() and it would probably mean that I need to access the database for each instance (also bad).
But maybe im missing something.
Php just seems different in how I think about class and objects. When should something be a class instance, and when not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个主观问题,因此我将尝试连贯地收集我的想法:
PHP 中的持久性具有不同的含义。您认为每个评论都应该是一个对象,因为评论具有可以对其执行的操作,这似乎是正确的。对象不会在页面加载过程中持续存在这一事实并不真正相关。在 PHP 中,在一个脚本中使用对象的情况并不罕见,该对象会在脚本完成时被销毁,然后在后续页面加载时重新实例化它。
面向对象编程提供了(除其他外)代码组织和代码重用。即使一个对象在脚本执行期间只真正使用一次,如果定义它的类有助于程序组织,那么您这样做是正确的。
通常,您不必担心资源浪费,直到它开始成为问题为止;如果您的服务器不断受到负担,从而降低了您的用户体验或限制了您的扩展,那么是时候进行优化了。
附录:
为您的评论定义类别的另一个原因是,当您需要扩展类别时,这样做可以带来好处。假设您决定实现评论回复之类的功能。回复本身只是一条评论,但包含有关它所引用的评论的一些额外属性。您可以扩展
Comment
对象来添加这些属性和附加功能。This is a subjective issue, so I'll try to gather my thoughts coherently:
Persistence in PHP has sort of a different meaning. Your thinking that each comment should be an object because comments have actions which can be performed on them seems correct. The fact that the objects won't persist across a page load isn't really relevant. It isn't uncommon in PHP to use an object in one script, which gets destroyed when the script completes, and then re-instantiate it on a subsequent page load.
Object-oriented programming provides (among other things) code organization and code reuse. Even if an object is only really used once during the execution of a script, if defining its class aids in program organization, you are right to do so.
You usually needn't worry about resource wastefulness until it starts to become a problem; if your server is constantly taxed to where it degrades your user experience or limits your expansion, then it is time to optimize.
Addendum:
Another reason defining a class for your comments is that doing so could pay dividends later when you need to extend the class. Suppose you decide to implement something like a comment reply. The reply is itself just a comment, but holds some extra attributes about the comment to which it refers. You can extend the
Comment
object to add those attributes and additional functionality.