views:

98

answers:

2

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 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. 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.

+1  A: 

Recommended reading: PHP References

Bandi-T
Counter-argument: http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html
Bill Karwin
@Bill Karwin: Hey Bill, that contains very good answers! Put that into an answer so that people can vote it up!
Bandi-T
While I would agree that references in PHP are poorly implemented the argument from that article could be summed up with: "Don't use references because they are hard"
Cfreak
@Cfreak - yeah, the author is basically arguing against using them, but all the situations he lists are in fact incorrect places to use references. The bottom line with PHP references is basically "don't try to use them explicitly for performance reasons, the interpreter already has that covered."
zombat
@Bill Karwin after reading that, I am 4x more confused now I think
jasondavis
I like PHP and use it all the time, but I just can't bring myself to bend my brain around the intricacies of how references and OO are implemented in it. I get the feeling I'd be better off bending my brain around how Python does it and my brain would end up less bent up in the end....
Alex JL
I think the answer by @zombat covers the issue. The blog by schlueters basically says the same thing, that you'd be better off using objects than references to scalars or arrays in PHP.
Bill Karwin
+4  A: 

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.

zombat
So is adding all these objects into a registry object and then injecting the registry object into all the places I need access to any of it's contents a good way to do this from your experience?
jasondavis
That approach sounds like it has advantages and disadvantages. Dependency injection by passing objects through constructor functions is definitely a way to make your classes easily testable with unit tests, so that's a big plus. If you have to constantly pass it through your code to every object however, that could get tedious, and perhaps a static and/or Singleton approach might serve you well. It's tough to say, it really depends on how (and how often) it's being used. I'd say if your approach is working fine, then don't mess with it too much. :)
zombat