简体   繁体   English

有关PHP对象性能的一些问题

[英]Some questions about the performance of PHP Objects

Right now since I am new to using Objects in PHP I feel like in my head, I think of a PHP object as being something big and bulky. 现在,由于我不熟悉在PHP中使用对象,因此在我的脑海中,我认为PHP对象是又大又笨重的东西。 This makes me want to use them less often, I feel Like I am taking really simple code and really over-complicating it by putting it into objects. 这使我想减少使用它们的次数,感觉就像我正在采用非常简单的代码,并通过将其放入对象中而使其过于复杂。

If I have a database, cache, session, core, and user object and I need to access them pretty much inside of each other and in other non-mentioned classes, I have decided to store all these inside a registry object. 如果我有数据库,缓存,会话,核心和用户对象,并且需要在彼此内部以及其他未提及的类中进行访问,那么我决定将所有这些都存储在注册表对象中。 So with my limited knowledge of how objects work, it would almost seem to me that by passing a registry object into a simple object is something really big. 因此,由于我对对象如何工作的了解有限,在我看来,将注册表对象传递到简单对象中似乎非常重要。 Like a registry is holding those 5 objects inside of it. 就像注册表在其中保留了这5个对象一样。 Is this wrong? 错了吗 Is the registy really only passing in a reference to where these objects are in memory? 区域记录真的仅传递了这些对象在内存中的位置的引用吗? Or am I really passing in a really BIG object into all my objects? 还是我真的将一个真正的BIG对象传递给我的所有对象?

Sorry if this makes no sense at all, hopefully it does. 抱歉,这根本没有意义,希望如此。 I am just trying to get a better understanding of how they work in relation to performance. 我只是想更好地了解它们在性能方面的工作方式。

In PHP5, all objects are passed by reference by default. 在PHP5中,默认情况下所有对象都是通过引用传递的。 In simple terms, a reference simply "points to" the actual object or variable's location in memory (be careful with terminology, as "pointers" are something quite different functionally from PHP's "references", but they are conceptually very similar). 用简单的术语来说,引用只是“指向”实际对象或变量在内存中的位置(请谨慎使用术语,因为“指针”在功能上与PHP的“引用”完全不同,但在概念上非常相似)。

When you pass objects around by reference, you are simply passing around very tiny memory indicators. 当通过引用传递对象时,您只是传递很小的内存指示符。 The objects themselves are not moved... they remain constant in memory, and aren't moved around or rewritten or anything. 对象本身不会被移动……它们在内存中保持不变,不会被移动或重写。 This includes when you put objects inside other objects... the references are simply adjusted. 这包括将对象放置在其他对象中时……只需调整引用。

The advantages that OO design and programming confer to your code usually far outweigh the minor overhead that comes with managing objects. OO设计和编程赋予代码的优势通常远远超过管理对象所带来的少量开销。 Rest assured that the PHP interpreter does it's best to optimally manage objects, and you're not incurring any more overhead by passing objects around than you would by passing references to integers or strings. 请放心,PHP解释器会做到最佳管理对象,这是最好的,与传递整数或字符串的引用相比,传递对象所带来的开销不会增加。 Reference overhead is very minimal. 参考开销非常小。

推荐阅读: PHP参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM