473,396 Members | 1,773 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,396 software developers and data experts.

PHP and returning references

Hi all,

I've noticed something strange with returning references, both in PHP4 and
5 - maybe it's supposed to do this, I don't know - but it seems a bit weird
nonetheless. Say we have the following test code:

<?php

class Test
{
var $foo = 12345;

function setFoo($foo=null)
{
$this->foo = $foo;
}

function &getFoo()
{
return $this->foo;
}
}

$test =& new Test();

$foo =& $test->getFoo();

$foo = 54321;

print_r($test);

?>

Upon execution, we should receive the following output:

test Object ( [foo] => 54321 )

....as we returned a reference to the foo attribute, and then changed it by
assigning 54321 to $foo. The PHP documentation is quite adamant that to
return by reference you need to have an ampersand both on the function
definition, and the variable assignment, and in this case that is true. If
you remove the ampersand from the getFoo() function definition above, you
will receive the following output:

test Object ( [foo] => 12345 )

....as a copy of the attribute and not a reference has been returned.

Now, what if we change the code to this:

<?php

class Test
{
var $foo = null; // <--- attribute is now NULL by default

function setFoo($foo=null)
{
$this->foo = $foo;
}

function &getFoo()
{
return $this->foo;
}
}

$test =& new Test();

$test->setFoo(12345); // <--- now using setter function to set foo
attribute to 12345

$foo =& $test->getFoo();

$foo = 54321;

print_r($test);

?>

Upon execution, we should receive the same result as before:

test Object ( [foo] => 54321 )

But if the ampersand is now *removed* from the getFoo() function definition,
it still returns by reference, and you receive the same result:

test Object ( [foo] => 54321 )

What am I missing?

--
{RainmakeR}
Jul 17 '05 #1
0 1425

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrew Fleet | last post by:
Hi, I'm looking at returning a reference to an array I create within a subroutine. I could do this... sub foo { my @theArray; <snip>
7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
2
by: matthias_k | last post by:
Hello, I'm wondering if returning const references to a class member is generally faster than returning a copy of the object. Consider this code: class A { std::string data; public:
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
3
by: Alfonso Morra | last post by:
I'm in the process of implementing an abstract factory design pattern for an application. I would like to know, which is the recommended way of returning objects from the factory - by reference or...
7
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
6
by: Derrick | last post by:
Hello all; Since I do have working code, this is more for my curiosity only. I'm creating a "Plugin" architecture, following some of the many examples on the 'net. Basically what I have is...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.