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

reference stays reference even if passed by value


Hello all,

I started porting a huge PHP application to PHP5 and encountered a
problem with references. I don't know whether the behavior I see is a
bug or a feature. The following nonsens test program should make clear
what's the problem:

class test {
public $_foo = null;

public function __construct($foo) {
$this->_foo = $foo;
}
}

function fct1(&$p) {
print "before call to fct2(): $p->_foo\n";
fct2($p);
print "after call to fct2(): $p->_foo\n";
}

function fct2($p) {
print "in fct2(): $p->_foo\n";
$p->_foo = "bla bla bla";
print "after modifying value parameter fct2(): $p->_foo\n";
}

$t = new test("Hello World!");
fct1($t);

If I evaluate this code with php v5.1.2 I get the following output:

before call to fct2(): Hello World!
in fct2(): Hello World!
after modifying value parameter fct2(): bla bla bla
after call to fct2(): bla bla bla

I don't understand this: fct1() takes a parameter as reference, so
fct1() should be able to change the argument I pass to it, but it does
not do this. Instead it call fct2(), a function that takes a "value
parameter". In my understanding, fct2() can change its argument, but
changes to not affect the caller, ie. changing the parameter should
not affect the variable passed to it in the callers scope.

Am I wrong or is this a (known) bug?

BTW: The PHP4 version works fine with php4.

Michael.

Jul 9 '07 #1
1 1191
Rik
On Mon, 09 Jul 2007 11:10:38 +0200, mi*************@tietoenator.com
<mi*************@tietoenator.comwrote:
>
Hello all,

I started porting a huge PHP application to PHP5 and encountered a
problem with references. I don't know whether the behavior I see is a
bug or a feature.
It's a change in PHP5: objects are passed by reference by default instead
of by value. If you need a copy you should use the clone keyword.

--
Rik Wasmus
Jul 9 '07 #2

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

Similar topics

7
by: EsC | last post by:
Hy! is it possible to pass function-arguments by reference? (for example in PHP you can use the "&" operator ... ) thx iolo
36
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
19
by: daniel | last post by:
This is a pretty basic-level question, but I'd really like to know, so thanks for any help or pointers you can provide (like what I would google for ;o) Suppose: <code> myFunc() {
11
by: Chameleon | last post by:
------- $a = array(............); $b = $a; ------- php says that $b points to $a until $b or $a changes. this means that the code above wants the same time with the code below: ------- $a = $a...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...

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.