note 97695 modified in language.references.arent by danbrown
| From: | danbrown@php.net | Date: | Tue, 04 May 2010 14:15:25 +0000 |
| Subject: | note 97695 modified in language.references.arent by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168925@lists.php.net to get a copy of this message | ||
The example shown is correct for scalars.
However, in PHP5 the example does not cover objects, which behave the opposite of the way this
documentation indicates.
It is my view that object references in PHP5 are exactly like pointers in C++.
This example code illustrates my point:
<?php
class Dog
{
var $Name;
function Dog($name)
{
$this->Name = $name;
}
function GetName()
{
return ($this->Name);
}
};
$bar = new Dog("Spot");
function foo(&$var)
{
$var = new Dog("Gypsy");
}
foo($bar);
echo "<p>".$bar->GetName();
echo "<p>"."If that said 'Gypsy', then object references are really
pointers";
?>
And the output is:
****
Gypsy
If that said 'Gypsy', then object references are really pointers
****
Therefore, corrected documentation should read (or similar):
"
Unless $bar is an object, there's no way to bind $bar in the calling scope to something else
using the reference mechanism since $bar is not available in the function foo (it is represented by
$var, but $var has only variable contents and not name-to-value binding in the calling symbol
table).
If $bar is an object, then you can change $bar in the calling scope using the reference mechanism
inside a function.
"
However, note that when I said:
"It is my view that object references in PHP5 are exactly like pointers in C++."
The "exactly like" part is misleading...
To clarify:
I don't mean PHP5 object references support pointer arithmetic or they contain memory
addresses.
I mean that, in PHP5, when you pass object variables to functions they *behave* exactly like
pointers to objects do in the same situation when writing in C++ (with the exception of the C++
features that just don't exist in PHP, such as pointer arithmetic).
Namely,
1. When you pass the object variable, the formal parameter behaves like a C++ pointer to the object
inside the function.
2. When you pass the object variable by reference, the formal parameter behaves like a C++ reference
to a pointer to the object inside the function.
The syntax is different between the languages, but the semantics are identical.
--was--
The example shown is correct for scalars.
However, in PHP5 the example does not cover objects, which behave the opposite of the way this
documentation indicates.
It is my view that object references in PHP5 are exactly like pointers in C++.
This example code illustrates my point:
<?php
class Dog
{
var $Name;
function Dog($name)
{
$this->Name = $name;
}
function GetName()
{
return ($this->Name);
}
};
$bar = new Dog("Spot");
function foo(&$var)
{
$var = new Dog("Gypsy");
}
foo($bar);
echo "<p>".$bar->GetName();
echo "<p>"."If that said 'Gypsy', then object references are really
pointers";
?>
And the output is:
****
Gypsy
If that said 'Gypsy', then object references are really pointers
****
Therefore, corrected documentation should read (or similar):
"
Unless $bar is an object, there's no way to bind $bar in the calling scope to something else
using the reference mechanism since $bar is not available in the function foo (it is represented by
$var, but $var has only variable contents and not name-to-value binding in the calling symbol
table).
If $bar is an object, then you can change $bar in the calling scope using the reference mechanism
inside a function.
"
http://php.net/manual/en/language.references.arent.php