note 102066 added to language.references.arent
| From: | briankatkappacsdotcom at osu1 dot php dot net | Date: | Tue, 25 Jan 2011 22:38:23 +0000 |
| Subject: | note 102066 added to language.references.arent | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176310@lists.php.net to get a copy of this message | ||
I think the terminology is tripping people up when it comes to assigning objects.
Try thinking of binding and references like this:
<?php
# Code:
$a = 5; $b =& $a; $c = new stdClass(); $d = $c;
# Behind the scenes symbol table and values:
$global_names = array(
'a' => array('binding' => 0),
'b' => array('binding' => 0),
'c' => array('binding' => 1),
'd' => array('binding' => 2),
);
$values = array(
0 => array('type' => 'scalar', 'value' => 5),
1 => array('type' => 'objId', 'value' => 0),
2 => array('type' => 'objId', 'value' => 0)
);
?>
$a is bound to (or references, or is a reference to) the value at index 0 (scalar 5).
$b is bound to the same thing as $a--the value at index 0 (scalar 5).
$c is bound to the value at index 1 (object ID 0).
$d is bound to the value at index 2 (a separate and distinct value also referring to object ID 0).
When the documentation states that you cannot [re-]bind $bar to something else from within the
example function foo, it means you can't change what in my pseudo-engine would be
$global_names['bar']['binding']. You can only change
$values[$names['var']['binding']] (using "$var ="; the same value
referenced/bound by $values[$global_names['bar']['binding']) or
$names['var']['binding'] (using "$var =&").
Also consider this code:
<?php
$a = 3; $b =& $a;
function foo (&$c) { $c = new stdClass(); }
function bar () { return new stdClass(); }
function &fum () { return new stdClass(); }
if (!is_object($a)) { echo "\$a does not initially refer to an object\n"; }
foo($b);
echo "\$b ", ($a === $b)? "has not": "has", " been re-bound by
foo\n";
if (is_object($a)) { echo "\$a now contains an object identifier\n"; }
$b =& bar();
echo "\$b ", ($a === $b)? "has not": "has", " been re-bound by
bar\n";
$b =& fum();
echo "\$b ", ($a === $b)? "has not": "has", " been re-bound by
fum\n";
?>
which outputs:
$a does not initially refer to an object
$b has not been re-bound by foo
$a now contains an object identifier
$b has not been re-bound by bar
$b has been re-bound by fum
In other words, the value can be changed but the binding does not (except for returning a
reference), exactly as stated.
Object identifiers do make object "values" work like pointers (but not to the extent of
C/C++, and not like references).
----
Server IP: 208.69.120.55
Probable Submitter: 99.114.241.185
----
Manual Page -- http://www.php.net/manual/en/language.references.arent.php
Edit -- https://master.php.net/note/edit/102066
Del: integrated -- https://master.php.net/note/delete/102066/integrated
Del: useless -- https://master.php.net/note/delete/102066/useless
Del: bad code -- https://master.php.net/note/delete/102066/bad+code
Del: spam -- https://master.php.net/note/delete/102066/spam
Del: non-english -- https://master.php.net/note/delete/102066/non-english
Del: in docs -- https://master.php.net/note/delete/102066/in+docs
Del: other reasons-- https://master.php.net/note/delete/102066
Reject -- https://master.php.net/note/reject/102066
Search -- https://master.php.net/manage/user-notes.php