Connecting Tech Pros Worldwide Help | Site Map

functions return by reference question

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 12:31 AM
lawrence
Guest
 
Posts: n/a
Default functions return by reference question

If I want a function to return by reference, I do this?

function & myCoolFunction() {
$queryObject = new queryObject();
return $queryObject;
}


I get back a reference to the object automatically now?

  #2  
Old July 17th, 2005, 12:31 AM
Fabian Wleklinski
Guest
 
Posts: n/a
Default Re: functions return by reference question

Hi Lawrence,
[color=blue]
> If I want a function to return by reference, I do this?
>
> function & myCoolFunction() {
> $queryObject = new queryObject();
> return $queryObject;
> }
>
>
> I get back a reference to the object automatically now?[/color]

Thats alright. But you have to use the &-operator a second
time when calling the function:

$referencedObject =& myCoolFunction();

(Yes. Really. It is ugly, but it is true. See [1].)


Greetings from Frankfurt/Germany,

Fabian Wleklinski


[1] http://www.php.net/manual/en/functio...ing-values.php


  #3  
Old July 17th, 2005, 12:37 AM
lawrence
Guest
 
Posts: n/a
Default Re: functions return by reference question

"Fabian Wleklinski" <Wleklinski.NNTP@eWorks.de> wrote in message[color=blue][color=green]
> > I get back a reference to the object automatically now?[/color]
>
> Thats alright. But you have to use the &-operator a second
> time when calling the function:
>
> $referencedObject =& myCoolFunction();
>
> (Yes. Really. It is ugly, but it is true. See [1].)[/color]


if I go:

$allEntries = & array_reverse($allEntries);

Then the array is returned by reference? PHP's memory usage is not
doubled in this exchange? At no point are copies made?

I need to do what I can to keep the memory to a minimum.
  #4  
Old July 17th, 2005, 12:38 AM
Fabian Wleklinski
Guest
 
Posts: n/a
Default Re: functions return by reference question

Hi lawrence,
[color=blue]
> if I go:
>
> $allEntries = & array_reverse($allEntries);[/color]

No, I don't think so, becauce array_reverse does not return
a reference, as far as I know.

Greetings from Frankfurt / Germany,

Fabian Wleklinski


  #5  
Old July 17th, 2005, 12:38 AM
John Downey
Guest
 
Posts: n/a
Default Re: functions return by reference question

Fabian Wleklinski wrote:
[color=blue]
> Hi lawrence,
>
>[color=green]
>>if I go:
>>
>>$allEntries = & array_reverse($allEntries);[/color]
>
>
> No, I don't think so, becauce array_reverse does not return
> a reference, as far as I know.
>
> Greetings from Frankfurt / Germany,
>
> Fabian Wleklinski
>
>[/color]
Well PHP wouldn't return a reference what he posted would work what it
would do is tell PHP to assign $allEntries to point to the memory
address of whatever array_reverse() was returning instead of copying the
data like it would normally do. In PHP you can pass arround Objects and
Arrays as refernces but passing them like array_reverse(&$allEntries);
is no longer needed or allowed unless you turn off the option in php.ini.

--
John Downey
http://delusive.dyn.ee
http://sage.dev.box.sk
http://blacksun.box.sk

  #6  
Old July 17th, 2005, 12:38 AM
Fabian Wleklinski
Guest
 
Posts: n/a
Default Re: functions return by reference question

Hi John, Hi Lawrence,
[color=blue]
> Well PHP wouldn't return a reference[/color]

Right.
[color=blue]
> what he posted would work what it would do is tell PHP to assign
> $allEntries to point to the memory address of whatever
> array_reverse() was returning instead of copying the data like it
> would normally do.[/color]

May be. That's what would happen in other languages. But I don't
know if PHP really copies the result two times: 1.) from a local
variable inside the method into the result (inside the stack as well),
2.) from the result into the assigned variable. May be PHP goes
another way. I haven't checked the php-source, so I cannot assume
this behaviour.
[color=blue]
> In PHP you can pass arround Objects and
> Arrays as refernces but passing them like array_reverse(&$allEntries);
> is no longer needed or allowed unless you turn off the option in php.ini.[/color]

Yes, PHP throws a warning if you do so. But Lawarence hasn't :-)

Greetings from Frankfurt / Germany,

Fabian Wleklinski


  #7  
Old July 17th, 2005, 12:38 AM
John Downey
Guest
 
Posts: n/a
Default Re: functions return by reference question

Fabian Wleklinski wrote:[color=blue]
> May be. That's what would happen in other languages. But I don't
> know if PHP really copies the result two times: 1.) from a local
> variable inside the method into the result (inside the stack as well),
> 2.) from the result into the assigned variable. May be PHP goes
> another way. I haven't checked the php-source, so I cannot assume
> this behaviour.[/color]
Well i am going off what I was told when I used to idle in #php on
irc.freenode.net

--
John Downey
http://delusive.dyn.ee
http://sage.dev.box.sk
http://blacksun.box.sk

 

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.