473,781 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11092
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:

$referencedObje ct =& 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:

$referencedObje ct =& 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.ne t

--
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
2076
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 renaming functions etc... I came across some recursive problems. (a basic construct can be found under the section BASIC CODE) These problems do not occur when renaming objects. (see section EXTRA CODE)
5
2693
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 function to verify the name fields, age, email and gender. My question is: if I create a function for each field like the code below, what would be the best way to organize the functions and call them? Would I need one main function and place...
8
3730
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 reference to the automatic variable from the inline function. I meant automatic variable by stack variable strictly local to that function. Thanks, Sreenivas.
6
1301
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 acceptable" as template parameters. My question is: Is there such thing as a reference to a function ? What exactly does it mean and how could I use such a reference? Has someone used this before ?
14
4003
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); } // reflect this vector off surface with normal vector
19
4261
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 char* formatter, ...); Then, I want to call it as so:
4
1583
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 discussed was: function object(o) { function F() {} F.prototype = o; return new F();
9
1919
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, next, i tried a single reference to the function, int ( & p ) ();
5
4663
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
1251
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 dangling. Therefore, this is bad: double& GetWeeklyHours() { double h =
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10143
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.