0

Since PHP objects are passed by reference(reference to the object itself, not to be confused with reference to the variable), does passing a very large object to a function add any resource stress to the system?

This seems like a 'get out of jail free card' for passing information, Since no copies are being made, I can pass a huge object around, while if I pass an array I take a memory hit.

In my OOP coding style, I prefer passing entire objects as arguments instead of just a single attribute, as it makes the code more readable and allows for greater functionality down the road if it's needed.

So my questions :

  1. Am I correct in my assumption that objects infer no resource hit when passed to functions?
  2. Does this scale? Is passing a 1MB object any different than passing a 10MB object?
  3. Is accessing an object attribute the same as accessing an array key? Is there a performance issue involved?

Thanks in advance!

5
  • PHP uses copy-on-write, so there's no memory hit for passing an array if you don't modify it. Commented Dec 25, 2014 at 22:12
  • Other than that, all your assumptions are correct. Commented Dec 25, 2014 at 22:13
  • I did not know that! Thanks! But if I do decide to modify the array, is there a difference? Commented Dec 25, 2014 at 22:13
  • Yes. It will make a copy of the entire array when you modify it. Commented Dec 25, 2014 at 22:18
  • Theoretical knowledge is good. But it will be better if you do familiar with memory_get_usage. Commented Dec 26, 2014 at 2:35

1 Answer 1

1
  1. Yes, passing by reference implies no resource hits.
  2. Yes, passing by reference makes no difference between 10 bytes and 10 megabytes.
  3. Yes, internally objects are actually just syntactically refined arrays. PHP is kinda stupid internally.

Bonus answer:

  1. Arrays are also passed by reference, but use a lazy-copy mechanism - they are not actually copied until modified. There is therefore no reason to prefer one over the other, they just fit different needs. In general, passing a 100MB array around to modify minor parts of it is never a good idea and just a consequence of bad decisions elsewhere. Also, if you really need to do this you can (and should) always pass by reference explicitly to eliminate that issue.

As for this line:

In my OOP coding style, I prefer passing entire objects as arguments instead of just a single attribute, as it makes the code more readable and allows for greater functionality down the road if it's needed.

I'm curious about what you mean with that. It sounds a lot like you're sacrificing OOP principles to cover up for architectural mistakes. As a rule of thumb, any interface to any object should pass the least possible information around. Just passing entire objects or arrays around 'for convenience' sounds like very bad architecture or coding style.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey! thanks for the answer! As to what made you suspecious(as it should), My point was that If i need 5 out of 20 object attributes passed as arguments, instead of passing an array, or 5 simple types, I just pass the entire object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.