By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,746 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,746 IT Pros & Developers. It's quick & easy.

functions return by reference question

P: n/a
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?
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Lawrence,
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?


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
Jul 17 '05 #2

P: n/a
"Fabian Wleklinski" <Wl*************@eWorks.de> wrote in message
I get back a reference to the object automatically now?


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].)

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.
Jul 17 '05 #3

P: n/a
Hi lawrence,
if I go:

$allEntries = & array_reverse($allEntries);


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
Jul 17 '05 #4

P: n/a
Fabian Wleklinski wrote:
Hi lawrence,

if I go:

$allEntries = & array_reverse($allEntries);

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

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

Jul 17 '05 #5

P: n/a
Hi John, Hi Lawrence,
Well PHP wouldn't return a reference
Right.
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.
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.
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.


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

Greetings from Frankfurt / Germany,

Fabian Wleklinski
Jul 17 '05 #6

P: n/a
Fabian Wleklinski wrote:
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.

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

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.