I've got a problem with linking classes together ... let me explain:
Imagine "class form{}" which is structured to hold information about a <form> tag. Within class form is an array that gets populated with calls to class form's addFieldset which creates an new instance of class fieldset{}. class fieldset{} holds information about a particular fieldset within a form. Within class fieldset{} is an array that gets populated with calls to class fieldset{}'s addInput function. addInput() creates an instance of class input{}.
Hopefully this isn't too convoluted so far. What we have up to now is class form{} containing an array of pointers to class fieldset{} which contains an array of pointers to class input{}.
Each class has a function render() defined which creates as much of the form as the class knows about, then calls the render() function on each object stored in its array ... so in theory, calling $form->render() should in turn render all the contained fieldsets and the inputs within that.
Now ... here's what happens. I call $form->render() and I get the <form> rendered as expected, and each of the fieldsets (which in this case merely provide section headers for the input fields). And that's where it stops. Nothing from class input{} gets rendered - when I do some scratting about at this point I find that the array in class fieldset{} containing the pointers to the input objects is empty.
Now here's the strange bit. If I call class fieldset{}'s render() function directly, without going through the form renderer, the input fields get rendered properly.
I'm stumped.
I'm fairly new to PHP, after recently migrating from JS/ASP. Running PHP4 under Apache 1.3.12 on W98. I expect there's an assumption I'm making about storing object pointers in arrays that doesn't work as I expected.
If you're still with me now after having waded through my explanation of the problem, thanks for reading and even more thanks for any suggestions!
Here's the code if it will help:
<?
/ Various bits modified for clarity. No actual <form> tags sent to the browser.
** Debugging output only.
/
$fm1 = new form("Test form");
$gp1 = $fm1->newGroup("Group 1");
$in1 = $gp1->newInput("Input 1");
$gp2 = $fm1->newGroup("Group 2");
$gp1->render();
// now replace $fm1->render() with $gp1->render() and see my problem ...
class form {
var $width;
var $method;
var $action;
var $items;
function form($label)
{
$this->label = $label;
}
function newGroup($label)
{
$retval = new formGroup($this,$label);
$this->items[sizeof($this->items)] = $retval;
return($retval);
}
function render()
{
print("Form <b>$this->label</b><p>");
for ($i=0;$i<count($this->items);$i++)
{
$this->items[$i]->render();
}
print("<p>Done rendering the form.<br>");
}
}
class formGroup {
var $parent;
var $label;
var $items;
function formGroup($parent,$label)
{
$this->parent = $parent;
$this->label = $label;
}
function newInput($label)
{
$retval = new formInput($this,$label);
$this->items[sizeof($this->items)] = $retval;
return($retval);
}
function render()
{
print("<table width=600 bgcolor=black><tr>");
print("<td><font color=white>$this->label</td>");
print("</tr></table>");
print(count($this->items) . " input fields:<br>");
for ($i=0;$i<count($this->items);$i++)
{
$this->items[$i]->render();
}
}
}
class formInput {
var $parent;
var $label;
var $data;
function formInput($parent,$label)
{
$this->parent = $parent;
$this->label = $label;
}
function render()
{
print("(input) $this->label<br>");
}
}