Jump to content

[SOLVED] apparent limitation with arrays of objects? (php4.4)


ss32

Recommended Posts

ok, i have this weird issue where an array within an array of objects does will not have any values added to it.

it is an HTML simulation engine that i am working on which will eventually allow me to generate fancy forms

 

ok, so i have the base classes, htmlObject and htmlContainer

class property {
var $attr;
var $value;

function property($attr, $value) {
	$this->attr = $attr;
	$this->value = $value;
}

function render() {
	return $this->attr . "=\"" . $this->value . "\"";
}
}

class htmlObject {
var $props;

function htmlObject($props = Array()) {
	$this->props = $props;
}

function findProperty($toFind) {
	foreach($this->props as $key => $i) {
		if ($i->attr == $toFind) {
			return $key;
		}
	}
	return false;
}

function setProperty($property, $value) {
	if (($k = $this->findProperty($property)) !== false) {
		$this->props[$k]->value = $value;
	}else{
		return false;
	}
}

function getProperty($property) {
	if (($k = $this->findProperty($property)) !== false) {
		return $this->props[$k];
	}
	return false;
}

function addProperty($property, $value) {
	if ($value != "" || $property == "id") {
		$this->props[] = new property($property, $value);
	}
}

function getValues() {
	$rval = "";
	foreach ($this->props as $i) {
		$rval .= $i->render() . " ";
	}
	return $rval;
}

function getElementById ($name) {
	if (($k = $this->getProperty("id")) !== false) {
		if ($k->value == $name) {
			return $this;
		}
	}
	return false;
}
}

class htmlContainer extends htmlObject {
var $elements;

function htmlContainer($e = Array()) {
	$elements = $e;
}

function addElement($e) {
	$this->elements[] = $e;
}

function getElementById($name) {
	if (($j = parent::getElementById($name)) !== false) {
		return $j;
	}else{
		foreach ($this->elements as $key => $i) {
			if (($k = $i->getElementbyId ($name)) !== false) {
				return $k;
			}
		}
	}
	return false;
}

function render() {
	if (is_array($this->elements) && count($this->elements) >= 0) {
		foreach ($this->elements as $i) {
			$i->render();
		}
	}else{
		echo " ";
	}
}
}

 

...and i also have the classes for the tables

class table extends htmlContainer {
var $rows;
var $columns;

function table ($id, $width = "100%", $cellpadding = "1", $cellspacing = "1", $border = "1") {
	parent::addProperty("id", $id);
	parent::addProperty("width", $width);
	parent::addProperty("cellpadding", $cellpadding);
	parent::addProperty("cellspacing", $cellspacing);
	parent::addProperty("border", $border);
	$this->rows = 0;
	$this->columns = 0;
}

function addRow() {
	$myId = parent::getProperty("id");
	$this->addElement(new tr($myId->value . "_" . $this->rows));
	$this->rows++;
}

function addColumn() {
	//$myId = $this->getProperty("id");
	foreach ($this->elements as $i) {
		$i->addCell($this->columns);
	}
	$this->columns++;
}

function getCell() {

}

function render() {
	echo "<pre>";
	var_dump($this);
	echo "</pre>";
	echo "\r\n<table " . parent::getValues() . ">";
	parent::render();
	echo "</table>";
}
}

class tr extends htmlContainer {
function tr ($id = "") {
	parent::addProperty("id", $id);
}

function addCell($num) {
	$myId = $this->getProperty("id");
	$ttd = new td($myId->value . "_" . $num);
	$this->addElement($ttd);
}

function render() {
	echo "\r\n<tr " . parent::getValues() . ">";
	parent::render();
	echo "</tr>";
}
}

class td extends htmlContainer {
var $mergedWith;
function td ($id, $width = "", $class = "", $style = "", $align = "left", $valign = "top", $colspan  = "1", $rowspan = "1") {
	parent::addProperty("id", $id);
	parent::addProperty("width", $width);
	parent::addProperty("class", $class);
	parent::addProperty("style", $style);
	parent::addProperty("align", $align);
	parent::addProperty("valign", $valign);
	parent::addProperty("colspan", $colspan);
	parent::addProperty("rowspan", $rowspan);
	$this->mergedWith = "";
}

function render() {
	echo "\r\n<td " . parent::getValues() . ">";
	parent::render();
	echo "</td>";
}
}

 

the whole engine relies on recursive-ish calls to the render methods of its contained elements.

the problem comes in when i call:

$tbl1 = new table("tbl");
$tbl1->addRow();
$tbl1->addColumn();

 

the dump of the variable "tbl1" contains this:

object(table)(4) {
  ...unimportant data (the property arrays and other instance variables)...
  ["elements"]=>
  array(1) {
    [0]=>
    object(tr)(2) {
      ["props"]=>
      array(1) {
        [0]=>
        object(property)(2) {
          ["attr"]=>
          string(2) "id"
          ["value"]=>
          string(6) "tid1_0"
        }
      }
      ["elements"]=>
      NULL
    }
  }
}

 

Is there a limitation?  am i doing something wrong? help )=

Link to comment
Share on other sites

Umm sorry for this triple post but the edit button for my previous post magically disappeared, so i cant edit.

 

the problem resided in the foreach loop of the method; i didnt know that foreach did not reference the element of the array that it was accessing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.