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 3 1471
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($obj ect->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
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($obj ect->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
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 ,&$v10) {
$count = func_num_args() ;
for ($i=1;$i<=$coun t;$i++){
if (gettype(${"v$i "})=='integ er') {
${"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_parameteriz ed (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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 function that sums
them up... hard coding them is tedious. How might I make this dynamic so
that it can handle any amount of expenses?
def tot_expenses(self, e0, e1, e2, e3):
pass
|
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) {
document.images.src = eval("mt" +menu+ ".src")
}
alert("imgOff_hidemenu");
hideMenu=setTimeout('Hide(menu,num)',500);
|
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 zbrac(float (*func)(float), float *x1, float *x2)
{
float f1,f2;
f1=(*func)(*x1);
|
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() { }
Client.prototype.fullname = "John Smith";
var s = "Client";
eval("var o = new " + s + "();");
alert(o.fullname);
|
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 of a sequence
of functions with arbitrary return types and/or parameters?
--
Randy Yates
Sony Ericsson Mobile Communications
Research Triangle Park, NC, USA
| |
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 values.
Works in both ie and firefox.
Parameters
styleType = top | left | width | height
toNumber = the new value of the style
then you pass in as many ids as you would like.
|
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
which is better. It has become what's known as “religious war” —
a heated fight over trivia. In this essay, i like to explain what is
the situation behind it, and which is proper.
|
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)
{
|
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.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |