Page 1 of 1

Trouble referencing methods with inheritance

Posted: Thu Mar 16, 2006 3:26 pm
by janjaap
I'm currently working on a form-generator and I'm encountering some problems with inheritance. I have constructed a base class, called HTML_element and a few other classes called Form, Fieldset, Input and so on that extend HTML_element.

My form class, as well as my Fieldset class, has a variable called $child_elements, which is an array of objects. In case of the Form class, these are instances of the Fieldset class and for the Fieldset class these are instances of the Input class.

Every class inherits the function printMarkup() (see below) from the base class. In this function, the object called (e.g. Form), calls the same function for all the objects in $child_elements. This however doesn't work; an empty string is returned instead of markup.

I haven't got a clue to as what I'm doing wrong. Anyone an idea?

Code: Select all

public function printMarkup() {
	$this->markupOutput .= "<$this->tagName";
	
	$this->markupOutput .= $this->getAttributes();
	
	$this->markupOutput .= '>'."\n";

	if (isset($this->legend)) $this->markupOutput .= '<legend>'.$this->legend.'</legend>'."\n";

	for ($i=0; $i<$count($this->child_elements); $i++) {
		$elementObj = $this->child_elements[$i];
		$elementObjMarkup = $elementObj->printMarkup();
		$this->markupOutput .= $elementObjMarkup;
	}

	$this->markupOutput .= "</$this->tagName>\n\n";
	
	return $this->markupOutput;
}

Posted: Thu Mar 16, 2006 3:40 pm
by feyd
Is this in PHP 4? The object may be getting cloned, possibly losing some data in the process if not programmed just right. I don't remember exactly how the cloning process works in 4 though. :|

Posted: Thu Mar 16, 2006 3:48 pm
by janjaap
Indeed using PHP 4.
I'm using the function below to assign elements to an object.

Code: Select all

public function addElement($elementObj) {
	$numChilds = count($this->child_elements);

	if (is_array($elementObj)) {
		for ($i=0; $i < count($elementObj); $i++) {
			$this->child_elements[$numChilds] = $elementObj[$i];					
		}
	}
	else {
		$this->child_elements[$numChilds] = $elementObj;
	}
}
The code for creating a form and adding elements is:

Code: Select all

$newForm = new Form('markupLayout', $_SERVER['PHP_SELF']);
$firstFF = new Fieldset('Op- en aanmerkingen');
$newForm->addElement($firstFF);
$newForm->printMarkup();