473,230 Members | 1,490 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

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?
Jul 17 '05 #1
6 11074
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: peter | last post by:
Hello all, Recently I've started to refactor my code ...(I'm using python 2.3.4) I tried to add extra functionality to old functions non-intrusively. When I used a construct, which involves...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
8
by: Sreenivas | last post by:
Hi, We cannot return a reference to an automatic variable from a function, as per the ANSI C++ standard the behaviour is undefined. Does this hold for inline functions too? or can I return a...
6
by: Timothy Madden | last post by:
Hello I have recently read in a fairy good book ('C++ Templates: The Complete Guide', by David Vandevoorde, Nicolai M. Josuttis) that "both references to objects and references to functions are...
14
by: Michael Sgier | last post by:
Hello If someone could explain the code below to me would be great. // return angle between two vectors const float inline Angle(const CVector& normal) const { return acosf(*this % normal); }...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
4
by: Daniel | last post by:
Hi, I was reading Douglas Crockford's article on prototypal inheritance at http://javascript.crockford.com/prototypal.html. I think it also relates to a post back in Dec 2005. The mechanism...
9
by: Rahul | last post by:
Hi Everyone, I was wondering about references to functions, so i tried this, int (& p) (); // doesn't work as array of references is illegal as memory is not allocated for references,...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
9
by: zebulon | last post by:
Hi, I am aware that when a function returns a reference, one should never return a local variable to that function, since that variable will be removed from the stack and the reference will be...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.