note 24069 modified in language.references.pass by spic
| From: | spic@php.net | Date: | Wed, 18 Sep 2002 19:01:26 +0000 |
| Subject: | note 24069 modified in language.references.pass by spic | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-36903@lists.php.net to get a copy of this message | ||
Since default argument passing is by COPYING the argument, instead of passing it by reference, it
might seem obvious to some people that passing a class handle or object instance variable would
create a COPY of the entire instance or object. But it was not obvious to me, since most languages
which pass by a method other than by reference, pass by VALUE, and the value of an instance is
almost always a handle pointing to (i.e. a pointer) the object. Thus, calls to class methods which
alter object/class variables will really alter those values in the passed object/class. PHP does
not do this. I spent longer than I care to admit trying to figure out why my class variables were
not retaining the values I was setting them to, only to discover that they were being set in COPIES
which then vanished into thin air. Examples:
// this does not work as expected
class Foo {
var $count = 0;
function add() {
$this->$count++;
}
function show() {
echo "count = " . $this->count . "\n";
}
}
function util( $handle ) {
$handle->add();
$handle->show(); // displays 1
}
$obj = new Foo;
util($obj);
$obj->show(); // displays 0!
Change the util() function defintion to use pass by reference and it works, i.e.:
function util( &$handle ) ...
Hope this helps someone else avoid banging their head for days on a really "simple"
problem. :-)
--was--
Since default argument passing is by COPYING the argument, instead of passing it by reference, it
might seem obvious to some people that passing a class handle or object instance variable would
create a COPY of the entire instance or object. But it was not obvious to me, since most languages
which pass by a method other than by reference, pass by VALUE, and the value of an instance is
almost always a handle pointing to (i.e. a pointer) the object. Thus, calls to class methods which
alter object/class variables will really alter those values in the passed object/class. PHP does
not do this. I spent longer than I care to admit trying to figure out why my class variables were
not retaining the values I was setting them to, only to discover that they were being set in COPIES
which then vanished into thin air. Examples:
// this does not work as expected
class Foo {
var $count = 0;
function add() {
$count++;
}
function show() {
echo "count = " . $this->count . "\n";
}
}
function util( $handle ) {
$handle->add();
$handle->show(); // displays 1
}
$obj = new Foo;
util($obj);
$obj->show(); // displays 0!
Change the util() function defintion to use pass by reference and it works, i.e.:
function util( &$handle ) ...
Hope this helps someone else avoid banging their head for days on a really "simple"
problem. :-)
http://www.php.net/manual/en/language.references.pass.php