Code Comments
Programming Forum and web based access to our favorite programming groups.I have created a user function that is passed a number of parameters and
a few of these must be passed by reference.
However, two of the parameters that are passed by reference are optional
- or at least I'd like them to be.
However, if I do this:
function foo($firstParm,$secondParm,&$thirdParm=false,&$lastParm=false)
{
if ($thirdParm)
.
if ($lastParm)
.
..
}
and call it like this:
foo("first","second");
I get "parse error, unexpected '=', expecting ')'
It seems that I cannot default variables that are defined as passed by
reference... or is there some provision for doing so that is a trick of
the trade?
I realize that it would work to pass in an array by reference to do the
same thing, but it would mean re-writing a lot of code.
Post Follow-up to this message"Mark Richards" <nospam@massmicro.com> wrote in message
news:425f1fd5$0$5889$9a6e19ea@news.newshosting.com...
>I have created a user function that is passed a number of parameters and a
>few of these must be passed by reference.
>
> However, two of the parameters that are passed by reference are optional -
> or at least I'd like them to be.
>
> However, if I do this:
>
> function foo($firstParm,$secondParm,&$thirdParm=false,&$lastParm=false)
> {
> if ($thirdParm)
> ..
> if ($lastParm)
> ..
>
> ...
> }
>
> and call it like this:
>
> foo("first","second");
>
> I get "parse error, unexpected '=', expecting ')'
>
> It seems that I cannot default variables that are defined as passed by
> reference... or is there some provision for doing so that is a trick of
> the trade?
>
> I realize that it would work to pass in an array by reference to do the
> same thing, but it would mean re-writing a lot of code.
This is a user-comment posted at php.net:
-----
Functions explicitly prototyped with formal parameters passed by reference
can't have default values. However, functions prototyped to assign default
values to formal parameters may be passed references.
For example, this is a parse error:
function foo(&$bar = null) {
// formal parameters as references can't have default values
$bar = 242;
}
While this is perfectly legal (and probably what you want, mostly):
function foo($bar = null) {
$bar = 242;
}
foo(); // valid call, no warnings about missing args
foo(&$x); // valid call, post $x == 242
-----
posted by bishop 03-Jun-2003 12:06
http://fi2.php.net/manual/en/functions.arguments.php
--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com
Post Follow-up to this messageMark Richards wrote: > I have created a user function that is passed a number of parameters and > a few of these must be passed by reference. > > However, two of the parameters that are passed by reference are optional > - or at least I'd like them to be. > > However, if I do this: > > function foo($firstParm,$secondParm,&$thirdParm=false,&$lastParm=false) ... > > It seems that I cannot default variables that are defined as passed by > reference... or is there some provision for doing so that is a trick of > the trade? > It's worth noting that there's no benefit in passing a simple data-type (e.g. booleans) by reference unless you want to alter the original variable. But in supplying default values, it's clear that you don't want to alter any existing variables. Therefore you should pass these parameters by value, not by reference. Passing by reference should only be used in situations where you want the function to alter its value, or where there would be a lot of overhead in creating a copy to send, e.g. a long string or a big array or object. -- Oli
Post Follow-up to this messageOllie and Kimmo,
Thank you for the suggestions.
The variables I was passing by reference are arrays. I posted an
inaccurate example.
I didn't realize that in php you can specify pass by reference "&" at
the function call. This actually makes a lot more sense (to me) and
follows C syntax.
Here's what I did:
Function definition:
function mpd($aDataX,$aDataY,$aDataYMidnight=0,$a
DataYNoon=0)
{
if (is_array($aDataYMidnight))
do something...
}
And here's the call:
mpd($aDataX,$aDataY,&$aDataYMidnight,&$aDataYNoon);
Or this one:
mpd($aDataX,$aDataY);
Both work nicely.
Thanks!
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.