Passing an object by reference.
| From: | William Bailey | Date: | Mon, 14 Apr 2003 13:03:40 +0000 |
| Subject: | Passing an object by reference. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-143758@lists.php.net to get a copy of this message | ||
Hi Guys,
Am trying to get into oop and have a problem. I have an object within an
object and i want the child object to be able to access the parent objects
variables/methods.
I haven't been able to find anything that lets be find out weather or not an
object is a part of another object so i have been trying ot pass the parent
object by reference to the child objects. I have been able to get it to make
a copy of the parent object and that seems to get passed on however i cant
get it to be passed by reference.
Below is the sample code i am using. After running it i'm expecting the
sectionID in all the objects to be '456' instead of '123' which is what it
was when the object was created but any changes don't seem to flow down the
chain. Can somebofy point out what i'm doing wrong please.
-->Code<--
class CMS_Section {
var $sectionId;
var $module;
function CMS_Section ( $id ) {
$this->sectionId = (integer)$id;
$this->module = new CMS_Page($this, $parameters);
}
}
class CMS_Module {
var $_section;
function CMS_Module ( &$section, $parameters ) {
// I want this to be a reference to the 'parent' object
$this->_section = &$section;
}
}
class CMS_Page extends CMS_Module {
var $_pageId;
function CMS_Page ( &$section, $parameters) {
parent::CMS_Module( &$section, $parameters );
$this->_pageId = (integer) $pageId;
print('Section id: ' .$this->_section->sectionId);
}
}
$a = new CMS_Section(123);
$a->sectionId = 456;
print('<p><pre>');
print_r($a);
--
Regards,
William Bailey.
Pro-Net Internet Services Ltd.
http://www.pro-net.co.uk
http://wb.pro-net.co.uk