Hi,
I'm using PHP 4.4.4 and attempting to write a new version of my website from scratch using OO quite strongly.
So my desire is to have the following:
- various objects that store the items I will display, e.g. Input Fields, Tables, Output Fields, etc. all from a common base.
- a page object that will store references to those fields so that I can have multiple page objects that will show the same output object (e.g. a main menu) or so the same output object can appear in the page multiple times.
To this end I thought I could have an array of object references in my page object. So I developed a simpler test.
(I have chopped a lot of superfluous functions here and reduced everything to essentials.)
Here is my standard display object:
class externalObject
{
var $fieldData;
var $name;
// Constructor
function externalObject($name, $fieldData) {
$this->name = $name;
$this->fieldData = $fieldData;
}
// Put data into the field
function putData($data) {
$this->fieldData = $data;
}
// Get data from the field
function getData() {
return $this->fieldData;
}
}
And here is are my two test classes (nested for reasons of testing) to store a reference to a version of the above object.
class testInternal
{
var $object;
// Constructor
function testInternal(&$objRef) {
$this->object = $objRef;
}
function getData() {
return $this->object->getData();
}
function putData($data) {
$this->object->putData($data);
}
}
class testExternal
{
var $intObject;
// Constructor
function testExternal(&$objRef) {
$this->intObject = new testInternal($objRef);
}
function getData() {
return $this->intObject->getData();
}
function putData($data) {
$this->intObject->putData($data);
}
}
Finally, here is the code I'm running. inputField is based on the External Object class above.
$text1 = new inputField('text1', 255, "Hello World");
$wowsers = new testInternal($text1);
echo $wowsers->getData();
echo "<BR>";
$wowsers->putData("No no no!");
echo $wowsers->getData();
echo "<BR>";
echo $text1->getData();
The problem is that $wowsers does not retain a reference to the $text1 object so that what shows up on screen here is:
Hello World
No no no!
Hello World
If I try the other way round and change the value of $text1 I also get no effect on the reference in $wowsers.
Can anyone help me get this to work? I realise I will have to store my inputField and other similar objects in the session or in some other way when I am moving around pages, btw.
Cheers
Theo