Re: Passing an object by reference.
| From: | William Bailey | Date: | Mon, 14 Apr 2003 13:28:55 +0000 |
| Subject: | Re: Passing an object by reference. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-143764@lists.php.net to get a copy of this message | ||
O.K. I have solved my problem. I have added a reference variable and it now
seems to work correctly. The problem before was that i was trying ot point a
reference to $this which only applies to the variables/methods within that
class and is not a general reference to the 'real' variable for that object.
To see what i mean look at the code below. If there are still any problems or
if there is a better way of doing it then please let me know.
-->code<--
<?php
class CMS_Section {
var $sectionId;
var $module;
function CMS_Section ( $id , $myReference) {
$this->sectionId = (integer)$id;
/*
* Load module and stuff to go here.
*/
$this->module = new CMS_Page($myReference, null);
}
}
class CMS_Module {
var $_section;
function CMS_Module ( &$sectionRef, $parameters ) {
$this->_section = &$sectionRef;
/*
* Load module information and parameters
*/
}
}
class CMS_Page extends CMS_Module {
function CMS_Page ( &$sectionRef, $parameters) {
parent::CMS_Module( &$sectionRef, $parameters );
/*
* This should not display anything because at this
* point the reference variable has not yet been fully
* defined.
*/
print('Section id: ' .$this->_section->sectionId);
}
}
error_reporting(E_ALL);
$a = new CMS_Section(123, &$a);
print('<p><pre>');
print_r($a);
$a->sectionId = 456;
print_r($a);
$a->sectionId = 789;
print_r($a);
?>
--
Regards,
William Bailey.
Pro-Net Internet Services Ltd.
http://www.pro-net.co.uk
http://wb.pro-net.co.uk