note 59611 added to language.references
| From: | cesoid at yahoo dot com | Date: | Sun, 11 Dec 2005 02:28:39 +0000 |
| Subject: | note 59611 added to language.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99937@lists.php.net to get a copy of this message | ||
Responding to post from nathan (who was responding to iryoku).
It is important to note the difference between what php is doing from the programmer's point of
view and what it is doing internally. The note that nathan refers to, about how (for example)
$something = $this makes a "copy" of the current object, is talking about making a
"copy" from the programmer's perspective. That is, for the programmer, for all
practical purposes, $something is a copy, even if internally nothing has been copied yet. For
example, changing the data in member $something->somethingVar will not change your current
object's data (i.e. it will not change $this->somethingVar).
What it does internally is a totally different story. I've tested "copying" an
object which contains a 200,000 element array, it takes almost no time at all until you finally
change something in one of the copies, because internally it only makes the copy when it becomes
necessary. The original assignment takes less than a millisecond, but when I alter one of the
copies, it takes something like a quarter of a second. But this only happens if I alter the 200,000
element array, if I alter a single integer of the object, it takes less than a microsecond again, so
the interpretter seems to be smart enough to make copies of some of the objects variables and not
others.
The result is that when you change a function to pass by reference, it will only become more
efficient if, inside the function, the passed variable is having its data altered, in which case
passing by reference causes your code to alter the data of the original copy. If you are passing an
object and calling a function in that object, that function may alter the object without you even
knowing, which means that you should pass an object by reference as long as it is ok for the
original copy to be effected by what you do with the object inside the function.
I think the real moral of the story is this:
1) Pass by reference anything that should refer to and affect the original copy.
2) Pass not by reference things that will definitely not be altered in the function (for an object,
it may be impossible to know whether it alters itself upon calling one of its functions).
3) If something needs to be altered inside a function without effecting the original copy, pass it
not by reference, and pass the smallest practical part that needs to change, rather than passing,
for example, a huge array of which one little integer will be altered.
Or a shorter version: Only pass things by reference when you need to refer to the original copy!
(And don't pass huge arrays or long strings when you need to change just a small part of them!)
----
Manual Page -- http://www.php.net/manual/en/language.references.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59611
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59611&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59611&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59611&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59611&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59611&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59611&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59611&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59611&report=yes
Search -- http://master.php.net/manage/user-notes.php