Problems with reference to caller in constructor
| From: | Craig Davey | Date: | Wed, 28 Apr 2004 01:05:04 +0000 |
| Subject: | Problems with reference to caller in constructor | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-184817@lists.php.net to get a copy of this message | ||
Hi Folks
I have created a User class that has a property named validation that contains an object of class Validation. I need my validation object to hold a reference to the 'parent' User class that created it. The following code demonstrates my best attempt and problem:
<?php
class User
{
var $validation;
var $firstname;
function User()
{
$this->validation = new Validation($this);
}
function set()
{
$this->firstname = 'Joe';
}
function save()
{
return $this->validation->run();
}
}
class Validation
{
var $parent;
function Validation(&$parent)
{
$this->parent = &$parent;
}
function run()
{
return $this->parent->firstname;
}
}
$u = new User();
$u->set(); // sets firstname to 'joe'
echo $u->save(); // returns null, I thought it should return 'joe'
?>
The validation object does hold a User object in it's parent property, but it is not tracking the changes to the user object which means it's not a reference. Is there a different way that I should be assigning the parent in order to get a reference instead of a copy?
- craig davey