From e330479f6648de97c6fc7c90d6da8d7716b736d7 Mon Sep 17 00:00:00 2001 From: Etienne Kneuss Date: Wed, 9 Jul 2008 00:19:48 +0000 Subject: [PATCH] Setting the record straight directly in the manual about object references git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@262311 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/oop5/references.xml | 87 ++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 language/oop5/references.xml diff --git a/language/oop5/references.xml b/language/oop5/references.xml new file mode 100644 index 0000000000..5906d31dc2 --- /dev/null +++ b/language/oop5/references.xml @@ -0,0 +1,87 @@ + + + + Objects and references + + One of the key-point of PHP5 OOP that is often mentioned is that + "objects are passed by references by default" This is not completely true. + This section rectifies that general thought using some examples. + + + + A PHP reference is an alias, which allows two different variables to write + to the same value. As of PHP5, an object variable doesn't contain the object + itself as value anymore. It only contains a object identifier which allows + object accessors to find the actual object. When an object is sent by + argument, returned or assigned to another variable, the different variables + are not aliases: they hold a copy of the identifier, which points to the same + object. + + + + References and Objects + + +$b->foo = 2; +echo $a->foo."\n"; + + +$c = new A; +$d = &$c; // $c and $d are references + // ($c,$d) = + +$d->foo = 2; +echo $c->foo."\n"; + + +$e = new A; + +function foo($obj) { + // ($obj) = ($e) = + $obj->foo = 2; +} + +foo($e); +echo $e->foo."\n"; + +?> +]]> + + &example.outputs; + + + + + +