note 64410 added to language.oop5.basic
| From: | edwardzyang at thewritingpot dot com | Date: | Thu, 13 Apr 2006 19:47:52 +0000 |
| Subject: | note 64410 added to language.oop5.basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-108012@lists.php.net to get a copy of this message | ||
I read the first few comments about references versus assignment on objects, and I still
couldn't get it. I'm going to talk about it in terms of practical usage, not the
tomfoolery occuring behind the scenes. No talk of memory pointers and the like.
<?php
// prepend this to all examples
class Mushroom
{
public $size;
public function __construct($size) {
$this->size = $size;
}
}
$mushroom = new Mushroom(1);
$assigned_shroom = $mushroom;
$reference_shroom =& $mushroom;
?>
Assignments and references, when it comes down to calling functions and modifying member variables,
don't make ANY difference at all.
<?php
$mushroom->size = 5;
// all shrooms are size 5
?>
The only deviation of behavior is when you assign totally new contents to a variable. Usually, this
never happens unless you're doing some strange reflection or injection.
<?php
$mushroom = 5;
// mushroom and reference_shroom are the integer 5, assigned_shroom is still a mushroom
?>
<?php
$reference_shroom = 5;
// same effect as example above
?>
<?php
$assigned_shroom = 5;
// only assigned_shroom is an integer
?>
The same would apply for any object type.
Once again, I reiterate, there's no need to overwrite variables and open this can of worms:
just make a new variable and be done with it. As long as you don't overwrite variables, it
doesn't matter whether or not you use references or assignments (unless you're planning
for PHP4 compatibility).
----
Server IP: 64.71.164.2
Probable Submitter: 68.197.162.150
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php
Edit -- http://master.php.net/note/edit/64410
Del: integrated -- http://master.php.net/note/delete/64410/integrated
Del: useless -- http://master.php.net/note/delete/64410/useless
Del: bad code -- http://master.php.net/note/delete/64410/bad+code
Del: spam -- http://master.php.net/note/delete/64410/spam
Del: non-english -- http://master.php.net/note/delete/64410/non-english
Del: in docs -- http://master.php.net/note/delete/64410/in+docs
Del: other reasons-- http://master.php.net/note/delete/64410
Reject -- http://master.php.net/note/reject/64410
Search -- http://master.php.net/manage/user-notes.php