Trouble referencing methods with inheritance

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
janjaap
Forum Newbie
Posts: 2
Joined: Thu Mar 16, 2006 3:06 pm
Location: Amsterdam

Trouble referencing methods with inheritance

Post 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;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :|
janjaap
Forum Newbie
Posts: 2
Joined: Thu Mar 16, 2006 3:06 pm
Location: Amsterdam

Post 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();
Post Reply