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. 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 i开发者_Python百科nto 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. 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?
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. 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).
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. 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. Reference overhead is very minimal.
Recommended reading: PHP References
精彩评论