Connecting Tech Pros Worldwide Forums | Help | Site Map

functions return by reference question

lawrence
Guest
 
Posts: n/a
#1: Jul 17 '05
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?

Fabian Wleklinski
Guest
 
Posts: n/a
#2: Jul 17 '05

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


lawrence
Guest
 
Posts: n/a
#3: Jul 17 '05

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.
Fabian Wleklinski
Guest
 
Posts: n/a
#4: Jul 17 '05

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


John Downey
Guest
 
Posts: n/a
#5: Jul 17 '05

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

Fabian Wleklinski
Guest
 
Posts: n/a
#6: Jul 17 '05

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


John Downey
Guest
 
Posts: n/a
#7: Jul 17 '05

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

Closed Thread