Re: Passing by references
| From: | Toby Butzon | Date: | Sun, 24 Dec 2000 04:38:01 +0000 |
| Subject: | Re: Passing by references | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-31738@lists.php.net to get a copy of this message | ||
What you're trying to do _can_ be done using PHP's references.
When PHP parses your code, it builds a array/hash which holds variables by
name and value (among other things which are insignificant to this
discussion, such as type, size, etc.). If you create a reference, PHP just
adds an additional key for the same value (hence the variable's contents are
not copied, and an alias is created).
If you are creating large variables within functions and want to keep from
copying the value to a new variable, _do_ return it as a reference AND
assign the function's return to the variable as a reference and you will not
be copying the data, but rather just creating an alias.
For example:
function &myFunction() {
// create variable you don't want to copy around
return $myVeryLargeVariable;
}
$myVeryLargeVariable =& myFunction();
You might also want to try benchmarking with & without this method to see
how much of an improvement there actually is.
Toby Butzon
[ criticism spurs improvement ]
----- Original Message -----
From: "A Complete Luser" <acompleteluser@penguinpowered.org.uk>
To: "php general list" <php-general@lists.php.net>
Sent: Saturday, December 23, 2000 9:38 PM
Subject: [PHP] Passing by references
: I've read the section of the php manual on references and found it a
: bit hard to follow. I am trying to clear the following up in my head.
:
: How do references work? the manual says that they are not like C
: pointers, but more like hardlinking in a file system... What does this
: mean for memory in an application where you have a large chunk of data
: that you want to pass back and forth to various functions in various
: library's?
:
: What I am doing at the moment is building an array of values in a
: function and then returning that array.
:
: AFAIK, this means that I am making a copy of that array, thus using
: double the amount of memory that I require. For a small group of
: users, this isn't a problem, but on a bigger system, this will hurt
: me.
:
: What I want to do, is to build the array in the function, and then
: return a pointer / reference to that array to the calling
: function. Then the calling function could access the values without
: having to make another copy in memory. The same goes for passing
: values to functions. I would like the calling function to pass
: references, not the actual values. So for example, instead of
:
: $result = foo($string1, $string2, $string3)
:
: function foo($string1, $string2, $string3)
: {
: $array[0] = $string1;
: $array[1] = $string2;
: $array[2] = $string3;
:
: return($array);
: }
:
: I would pass pointers to string1, string2 and string3 to function foo,
: and then receive a pointer to the array containing the result. The
: example above is a dumb example, with no need to use a function, but
: it's the easist way I can illustrate my point.
:
: Is what I want to do possible? if so, could someone please point me to
: where?
:
: Thanks,
:
: --
: A Complete Luser <acompleteluser@penguinpowered.org.uk>
:
: --
: PHP General Mailing List (http://www.php.net/)
: To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
: For additional commands, e-mail: php-general-help@lists.php.net
: To contact the list administrators, e-mail: php-list-admin@lists.php.net
:
: