Re: Argument passed by reference?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



tedd wrote:
It's not that it's not allowed, it's that it should be done differently. You should make the function accept a reference in the definition, not pass in a reference to the function.

The manual page has a good, simple example on what to do.

http://www.php.net/manual/en/language.references.pass.php


Chris:

Interesting. One would think (at least I do) it should be the other way around.

Using the example given at the link above:

<?php
function foo(&$var)
{
   $var++;
}

$a=5;
foo($a);
// $a is 6 here
?>

We have one function that works one way.

<?php
function foo($var)
{
   $var++;
}

$a=5;
foo(&$a);
// $a is 6 here
?>

However, doing it this way, the function can serve two purposes. I can send it a reference or I could send it a value. I know that in this function a value doesn't do anything, but it could if the function was different.

Plus, in the first function, I can't send it a reference (i.e., a reference to a reference?).

I'd guess it's to simplify things.

Having been involved in a big cms that used references all over the place (everything was OO), it was extremely hard to find / track down where they should be and where they were missing - which caused memory blow-outs where they were not used, and segfault crashes where they were incorrectly used.

If they were all in the function definitions, I don't have to worry about that particular problem.

However, it's something the developers would have to give a definitive answer on.

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux