Connecting Tech Pros Worldwide Help | Site Map

Passing arguments by reference to functions

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:35 AM
Martin Lucas-Smith
Guest
 
Posts: n/a
Default Passing arguments by reference to functions



Having re-read www.php.net/functions.arguments recently, the notion of
passing arguments by reference to a function makes a lot more sense now.

However, my question is: is there any difference in outcome between:



function add_some_extra(&$string) {
$string .= 'and something extra.';
}

add_some_extra($str);



and


function add_some_extra($string) {
$string .= 'and something extra.';
return $string;
}

$str = add_some_extra($str);


other than presumably that the former is less memory-intensive?


Martin


  #2  
Old July 17th, 2005, 01:35 AM
Sandy Lewanscheck
Guest
 
Posts: n/a
Default Re: Passing arguments by reference to functions

Martin Lucas-Smith wrote:[color=blue]
>
> Having re-read www.php.net/functions.arguments recently, the notion of
> passing arguments by reference to a function makes a lot more sense now.
>
> However, my question is: is there any difference in outcome between:
>
>
>
> function add_some_extra(&$string) {
> $string .= 'and something extra.';
> }
>
> add_some_extra($str);
>
>
>
> and
>
>
> function add_some_extra($string) {
> $string .= 'and something extra.';
> return $string;
> }
>
> $str = add_some_extra($str);
>[/color]

Hi,

there is no difference really i would say.
Calling by Reference also doesn't make much
sense here.
But imagine you have an object you would like
to manipulate inside another object.
There you would pass the other object by reference.
For instance if you have an object car and inside you
want to create 4 objects of the type wheel.
Then i would make the wheels and save them
as References.
Otherwise you would lose them
after you leave the car constructor...
ehmmm... jeez... :) that sounds confusing...? :)

Sandy

  #3  
Old July 17th, 2005, 01:36 AM
Jochen Buennagel
Guest
 
Posts: n/a
Default Re: Passing arguments by reference to functions

Martin Lucas-Smith wrote:
[color=blue]
> other than presumably that the former is less memory-intensive?[/color]

It is not. It has something to do with the extra smart
copy-on-write-reference-counting mechanism that the Zend engine uses.
You can read it up in "Core PHP", which also has a full explanation of
the reasons under the heading "Don't Trust Your Instincts".

But much worse is IMHO the fact that you can't use literals as
arguments, like somefunction(array('a','b')).

Jochen

  #4  
Old July 17th, 2005, 01:36 AM
Savut
Guest
 
Posts: n/a
Default Re: Passing arguments by reference to functions

it just depend on what kind of treatement you want to do on the object passed to the function.
If you want to treat the same object than the original then you use reference, if you are about treating a copy, then you dont use
reference. If you pass a car to the function using reference then if inside the function you change the wheels, then when you exit
the function, the original car will also have the wheels changed.

Savut

"Jochen Buennagel" <zang@buennagel.com> a écrit dans le message de news:braa68$sad$02$1@news.t-online.com...[color=blue]
> Martin Lucas-Smith wrote:
>[color=green]
> > other than presumably that the former is less memory-intensive?[/color]
>
> It is not. It has something to do with the extra smart
> copy-on-write-reference-counting mechanism that the Zend engine uses.
> You can read it up in "Core PHP", which also has a full explanation of
> the reasons under the heading "Don't Trust Your Instincts".
>
> But much worse is IMHO the fact that you can't use literals as
> arguments, like somefunction(array('a','b')).
>
> Jochen
>[/color]


  #5  
Old July 17th, 2005, 01:36 AM
Bruno Desthuilliers
Guest
 
Posts: n/a
Default Re: Passing arguments by reference to functions

Martin Lucas-Smith wrote:[color=blue]
>
> Having re-read www.php.net/functions.arguments recently, the notion of
> passing arguments by reference to a function makes a lot more sense now.
>
> However, my question is: is there any difference in outcome between:
>
>
> function add_some_extra(&$string) {
> $string .= 'and something extra.';
> }
>
> add_some_extra($str);
>
>
>
> and
>
>
> function add_some_extra($string) {
> $string .= 'and something extra.';
> return $string;
> }
>
> $str = add_some_extra($str);
>[/color]

There's at least three *major* diffs : the second version
- does not have side-effect, which means you *don't* modify the argument
(and this is a Good Thing(tm)).

- return something, which means you can do thangs like :
$mystr ="here is a string ";
print add_some_extra($mystr)

- can be used with litterals, ie :
print add_some_extra("here is another string");

So the second version is IMHO *much* more clean and useful.
[color=blue]
> other than presumably that the former is less memory-intensive?[/color]

Here you maybe wrong... (see other posts in this thread).

Bruno

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.