You are here: Re: trying to pass by reference to an optional function parameter « PHP Language « IT news, forums, messages
Re: trying to pass by reference to an optional function parameter

Posted by Xenophaw on 06/13/05 02:51

"John T" <johntutton@sbcglobal.net_nospam> wrote:
> Thanks for pointing that out. However, I still get a warning when I do it
> that way.
>
> How about passing an array parameter, then assigning a default value to
> the
> array? I know some languages automatically pass all arrays as references.
> Does PHP do this? I attempted this, but it didn't work, but maybe I'm
> doing
> it wrong.
>
> Thanks,
> JT
>
> "Kimmo Laine" <eternal.erectionN05P@Mgmail.com> wrote in message
> news:Nifpe.3326$l24.1956@reader1.news.jippii.net...
>> "John T" <johntutton@sbcglobal.net_nospam> wrote in message
>> news:Licpe.588$%j7.389@newssvr11.news.prodigy.com...
>> >I am trying to make a function that takes an optional parameter that
>> >gets
>> > passed by reference.
>> >
>> > Here is the first line of my function definition:
>> >
>> > function funQueryDatabase($strQuery, &$intInsertId = NULL) {
>> >
>> > I am getting this error:
>> >
>> > Parse error: parse error, expecting `')'' in c:\program
>> > files\easyphp1-8\www\my_query_database_function.php on line 7
>> >
>> > Line 7 is the first line of my function definition (above).
>> >
>> > If I take out the & or if I take out the = NULL, then the error goes
> away,
>> > but of course it doesn't do what I want.
>> >
>> > Is it not possible to have an optional pass-by-reference parameter, or
> is
>> > there another value that I should use for the default value?
>>
>>
>> This has been disuceesd in The Manual's user comments. Here's a post on
> that
>> topic by "bishop" quoted from
>> http://fi.php.net/manual/en/functions.arguments.php (wouldn't hurt to
>> take
> a
>> look at the entire page)
>>
>> "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
>> "
>>
>> And this has been asked in here also earlier...
>>
>> --
>> Welcome to Usenet! Please leave tolerance, understanding
>> and intelligence at the door. They aren't welcome here.
>> eternal piste erection miuku gmail piste com
>>
>>
>
>

PHP doesn't pass arrays or objects by reference for default (well, at least
not until PHP 5, if I remember well). If you want to use an optional
argument, you can use the func_get_arg() passing the argument number you
want (0 for the first). In that case the function would get:

function funQueryDatabase($strQuery /*, &$intInsertId*/) {
/*...*/
if ((func_num_args() > 1) {
$id =& func_get_arg(1);
/*...*/
}
/*...*/
}

And you could call it with funQueryDatabase($str, 10) or
funQueryDatabase($str, &$id).
In this way you avoid the need to pass a variable name for the second
parameter (required if the formal parameter is in the form &$id, which would
not permit to use a number or a string).


I hope this answers to your question.
--
Xenophaw

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация