Which is better, pass, or not pass by reference? – PHP

  php

Q(Question):

Folks,

I’ve asked this before but never got any response but its important and I
thought I’d pitch it again (hopefully a bit clearer)…

One can pass data from one function to another either by passing a copy, or
passing by reference.

My understanding of passing by reference (putting the & before a variable
during the function declaration) means that the original data is used.

My understanding of *not* passing by reference, (thus passing a copy)
literally
means creating a copy of the data contained in my variables – thus doubling
the memory usage for this data… even if its just a temporary while the
function is being executed.

I *believed* (past tense) that one should only pass by reference if they
wanted the child function to change the data and pass the newer values back
to the parent function.

I’m now thinking that this is just a feature and not the only usage. I am
begining to think I should use "pass by reference" nearly all the time in
order to conserve memory (since I won’t be creating copies of my variable
data in memory).

Would this be good practice? Am I right that passing by reference means that
my variable data is not duplicated thus using less memory than if I had
passed my variable data as a copy?

All comments + replies please via the newsgroup,
Thanks,
Randell D.

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?

A(Answer):

"Randell D." <yo**************@randelld.at.yahoo.com> schreef in bericht
news:cU4lb.138072$6C4.80562@pd7tw1no…

Folks,

I’ve asked this before but never got any response but its important and I
thought I’d pitch it again (hopefully a bit clearer)…

One can pass data from one function to another either by passing a copy,
or passing by reference.

My understanding of passing by reference (putting the & before a variable
during the function declaration) means that the original data is used.

My understanding of *not* passing by reference, (thus passing a copy)
literally
means creating a copy of the data contained in my variables – thus
doubling the memory usage for this data… even if its just a temporary while the
function is being executed.

I *believed* (past tense) that one should only pass by reference if they
wanted the child function to change the data and pass the newer values
back to the parent function.

I’m now thinking that this is just a feature and not the only usage. I am
begining to think I should use "pass by reference" nearly all the time in
order to conserve memory (since I won’t be creating copies of my variable
data in memory).

Would this be good practice? Am I right that passing by reference means
that my variable data is not duplicated thus using less memory than if I had
passed my variable data as a copy?

All comments + replies please via the newsgroup,
Thanks,
Randell D.

Passing by reference is usefull if you want a function to return more than
only one value… a function returns normally only one value (although you
can return an array with multiple results too)…

Using a reference is done if you want to change a var outside the scope of
your function….

Yes, interesting, why not always use call by reference??? Only thing i can
think is that you do not always want to change values outside the
function…

Regards,

Marcel

A(Answer):

"Randell D." <yo**************@randelld.at.yahoo.com> wrote in message
news:cU4lb.138072$6C4.80562@pd7tw1no…

I’m now thinking that this is just a feature and not the only usage. I am
begining to think I should use "pass by reference" nearly all the time in
order to conserve memory (since I won’t be creating copies of my variable
data in memory).

_if_ I am correct, php 5 will pass by reference by default.

rush

http://www.templatetamer.com/

A(Answer):

Reference is useful only for object.

if your code is object oriented, then you should always return
references to your objects, never copies, or you will have some
surprises (change the object in one place, without changing it somewhere
else is bad)

for built-in types like strings, integers, booleans , return copies, you
will double the use of memory but for this kind of elements, it has
absolutly NO importance (I’m not even sure you can reference a string
for example).

Now even if your code is not OO, you still have some php functions
returning objects i.e. pg_connect, mysql_result… a connection resource
is an object, a resultset is an object, then if you manipulate them or
return them in some functions, return the reference, not the copy.

another good idea to save your memory is to unset your objects once your
done with ’em, like one would do in C++ maybe.

.b

A(Answer):

Randell D.:

Folks,

I’ve asked this before but never got any response but its important and I
thought I’d pitch it again (hopefully a bit clearer)…

One can pass data from one function to another either by passing a copy,
or passing by reference.

My understanding of passing by reference (putting the & before a variable
during the function declaration) means that the original data is used.

My understanding of *not* passing by reference, (thus passing a copy)
literally
means creating a copy of the data contained in my variables – thus
doubling the memory usage for this data… even if its just a temporary
while the function is being executed.

I *believed* (past tense) that one should only pass by reference if they
wanted the child function to change the data and pass the newer values
back to the parent function.

I’m now thinking that this is just a feature and not the only usage. I am
begining to think I should use "pass by reference" nearly all the time in
order to conserve memory (since I won’t be creating copies of my variable
data in memory).

Would this be good practice? Am I right that passing by reference means
that my variable data is not duplicated thus using less memory than if I
had passed my variable data as a copy?

That’s very risky business IMO. At some point you’re bound to forget about
it, and change a variable that was passed by reference. This can lead to
lots of hard to catch bugs. It is generally accepted that pointers are a
bad thing, and they should be avoided whenever you can. Functional
programming languages go farthest in this respect, and this also makes them
easier to program.

Also note that the PHP engine has some tricks up it’s sleeve to avoid
unnecessary copying (http://www.zend.com/zend/art/ref-count.php), but I’m
not sure if this works with argument passing as well.

All in all I don’t think it’s worth the hassle. If memory is a problem you
should instead find the memory hotspots and optimize them, possibly
rewriting them in C.

André Næss

A(Answer):

Carved in mystic runes upon the very living rock, the last words of odel
of comp.lang.php make plain:

Reference is useful only for object.

That’s not true. Passing by reference is useful if you want the function to
modify the variable you send to it.
(I’m not even sure you can reference a string for example).

Of course you can.


Alan Little
Phorm PHP Form Processor
http://www.phorm.com/

A(Answer):

Alan Little didn’t write:

odel wrote:

but Alan Little wrote:

(I’m not even sure you can reference a string for example).

Of course you can.

You can pass all variables by reference.
What you can’t pass by reference are constants:

<?php
function xyz(&$p1, &$p2, &$p3) {
// do nothing
}

$s = ‘string’;
$i = 7;
xyz($s, $i, ‘ERROR HERE’);
?>


I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.

LEAVE A COMMENT