473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Grabbing Arbitrary Amount of Function Arguments by Reference

Hello everyone:

Recently, I've come across the need to accept an arbitrary amount of
arguments in my function, but I also need to alter the data in the
calling scope.

I have been trying to toy around with the func_get_args function,
however, it seems the data is copied before I can access it, as I
tried to globalize the values in the argument array and use
references, but neither worked.

I would be grateful if anyone could point me in the right direction or
provide any illumination on this subject.

Curtis
Apr 1 '07 #1
3 1451
Curtis wrote:
I have been trying to toy around with the func_get_args function,
however, it seems the data is copied before I can access it, as I
tried to globalize the values in the argument array and use
references, but neither worked.
As a workaround, you can pass the function a single array or object by
reference:

function modify(&$object) {
$object->a = strtoupper($object->a);
}

$object = new stdClass;
$object->a = 'a';
modify($object);
print $object->a; // A

Note that when passing an object, it's passed by reference by default in PHP
5.
JW
Apr 1 '07 #2
Janwillem Borleffs wrote:
Curtis wrote:
>I have been trying to toy around with the func_get_args function,
however, it seems the data is copied before I can access it, as I
tried to globalize the values in the argument array and use
references, but neither worked.

As a workaround, you can pass the function a single array or object by
reference:

function modify(&$object) {
$object->a = strtoupper($object->a);
}

$object = new stdClass;
$object->a = 'a';
modify($object);
print $object->a; // A

Note that when passing an object, it's passed by reference by default in PHP
5.
JW

Ahh, thank you, I like your approach. Not sure why my mind was set on
varargs.

Thanks for the help,
Curtis, http://dyersweb.com
Apr 1 '07 #3
Curtis wrote:
Hello everyone:

Recently, I've come across the need to accept an arbitrary amount of
arguments in my function, but I also need to alter the data in the
calling scope.

I have been trying to toy around with the func_get_args function,
however, it seems the data is copied before I can access it, as I tried
to globalize the values in the argument array and use references, but
neither worked.

I would be grateful if anyone could point me in the right direction or
provide any illumination on this subject.

Curtis
Not so elegant as the solution Janwillem provided, but a useable
workaround I once used:
<?PHP
function foo (&$v1,&$v2,&$v3,&$v4,&$v5,&$v6,&$v7,&$v8,&$v9,&$v1 0) {
$count = func_num_args();
for ($i=1;$i<=$count;$i++){
if (gettype(${"v$i"})=='integer') {
${"v$i"}+=1;
}
}
return true;
}

$a=1;
$b=3;
$c=5;
$d=8;
$e='bar';
@foo ($a,$b,$c,$d,$e); // suppress warnings about missing parameters
echo "$a $b $c $d $e";
// outputs 2 4 6 9 bar
?>

I haven't been able -like you- to alter parameter values when using the
non_parameterized (is that English?) version of a function and
func_get_args(). Perhaps with some devious trickery it is possible to
find the referenced variables somewhere, somehow, but my guess is that
would not be a pure PHP solution.

Of ocurse the function is useless, but with some smart
type-checking-and-branching real nifty stuff could be constructed. I
haven't tested what the practial parameter list length limit is.

HTH

Sh
Apr 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: rbt | last post by:
How do I set up a function so that it can take an arbitrary number of arguments? For example, I have a bunch of expenses which may grow or shrink depending on the client's circumstance and a...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: Honestmath | last post by:
Hello everyone, I'm using a root finding algorithm in a function that only takes as arguments a function pointer and two variables that represent guesses at the roots of the function. int...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
3
by: Randy Yates | last post by:
Hi, We know we can build arrays of variables of the same type and arrays of functions of the same "type" (i.e., same return value and same parameters), but is there a way to automate the calling...
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
0
by: John O'Hagan | last post by:
On Tue Sep 30 11:32:41 CEST 2008, Steven D'Aprano Thanks, both to you and Bruno for pointing this out, I'll certainly be using it in future.
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.