Re: use of = &new object versus = new object
| From: | Joseph Crawford | Date: | Mon, 11 Jul 2005 18:32:11 +0000 |
| Subject: | Re: use of = &new object versus = new object | ||
| References: | 1 2 3 4 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-5975@lists.php.net to get a copy of this message | ||
i take it you have never used a pointer, i will try to explain this the best
i can it may not be exact in php but it will be similar.
when you use the & (reference operator) it still holds a value in the
variable such as
$db = new Database();
$mydb = &$db;
when you do this $db is created and holds the actual object. Now when you do
$mydb = &$db;
it stores the memory location to where $db is, rather than the actual data.
It just "points" to the other memory location.
say you have this
<?php
class Person {
var $name;
var $age;
function Person($name, $age) {
$this->name = $name;
$this->age = $age;
}
function name($name = '') {
if($name != '') $this->name = $name;
return $this->name;
}
}
$user = new Person('joe', 25);
$myuser = &$user
$myuser->name('fred');
echo $user->name();
echo $myuser->name();
?>
notice here that when i echo the value i am using $user and not $myuser,
when you use the reference operator you actually modify the original object
not the copy. Try this with the & and then without to see the differences.
--
Joseph Crawford Jr.
Codebowl Solutions, Inc.
1-802-671-2021
codebowl@gmail.com