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.

.



Relevant Pages

  • A little OT maybe
    ... down an app. ... in the IDE. ... "The function your trying to use is on a CD bla bla. ... To find the bug in time, isn't that what we all hope for. ...
    (comp.lang.basic.visual.misc)
  • Re: A little OT maybe
    ... It sounds like you need to reinstall whatever it is. ... If you don't figure it out you might try posting ... "The function your trying to use is on a CD bla bla. ... To find the bug in time, isn't that what we all hope for. ...
    (comp.lang.basic.visual.misc)