note 58331 added to functions.arguments
| From: | SergioSantana:ssantana at tlaloc dot imta dot mx | Date: | Mon, 31 Oct 2005 22:59:12 +0000 |
| Subject: | note 58331 added to functions.arguments | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97896@lists.php.net to get a copy of this message | ||
PASSING A "VARIABLE-LENGTH ARGUMENT LIST OF REFERENCES" TO A FUNCTION
As of PHP 5, Call-time pass-by-reference has been deprecated, this represents no problem in most
cases, since instead of calling a function like this:
myfunction($arg1, &$arg2, &$arg3);
you can call it
myfunction($arg1, $arg2, $arg3);
provided you have defined your function as
function myfuncion($a1, &$a2, &$a3) { // so &$a2 and &$a3 are
// declared to be refs.
... <function-code>
}
However, what happens if you wanted to pass an undefined number of references, i.e., something like:
myfunction(&$arg1, &$arg2, ..., &$arg-n);?
This doesn't work in PHP 5 anymore.
In the following code I tried to amend this by using the
array() language-construct as the actual argument in the
call to the function.
<?php
function aa ($A) {
// This function increments each
// "pseudo-argument" by 2s
foreach ($A as &$x) {
$x += 2;
}
}
$x = 1; $y = 2; $z = 3;
aa(array(&$x, &$y, &$z));
echo "--$x--$y--$z--\n";
// This will output:
// --3--4--5--
?>
I hope this is useful.
Sergio.
----
Manual Page -- http://www.php.net/manual/en/functions.arguments.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58331
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58331&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58331&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58331&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58331&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58331&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58331&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58331&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58331&report=yes
Search -- http://master.php.net/manage/user-notes.php