why doesn't this following work:
the following is a file with two classes, the first, an object that contains an array of objects. the second is any old object.
<?php
class Array_Of_Objects
{
var $count;
var $obj_list;
function Array_Of_Objects()
{
$this->count=0;
}
function Add_Obj($obj)
{
$this->obj_list[$this->count++] = $obj;
echo "object added, count = " . $this->count . "<br/>";
}
function Get_Obj($i)
{
return $this->obj_list[$i];
}
} //end of AOO class
class Some_Object
{
var $v;
function Some_Object($parent,$p)
{
$this->v = $p;
$parent->Add_Obj($this);
}
}
?>
I then have a debug file below. It creates the array of objects object, creates three some objects, and of course some objects insert themselves into the array of objects in its constructor. you can verify they are being inserted with a few echo statements if you like.
<?php
require_once('aoo.class');
include_once('dump.include');
$object_list = new Array_Of_Objects();
$obj1 = new Some_Object(1);
$obj2 = new Some_Object(2);
$obj3 = new Some_Object(3);
dump($object_list,"Object List");
for ($i=0; $i <3; $i++)
dump($object_list->Get_Obj($i),"Object");
?>
I then have a short function to dump the contents of objects in key value pairs:
<?php
function dump($obj,$s)
{
echo "<br/>" . $s . "<br/>";
$arr = get_object_vars($obj);
while (list($prop, $val) = each($arr))
echo "\t$prop = $val\n";
}
?>
here's the output from running the debug php:
object added, count = 1
object added, count = 1
object added, count = 1
Object List
count = 0 obj_list =
Object
Warning: Variable passed to each() is not an array or object in c:\program files\apache group\apache\htdocs\golf\dump.include on line 7
Object
Warning: Variable passed to each() is not an array or object in c:\program files\apache group\apache\htdocs\golf\dump.include on line 7
Object
Warning: Variable passed to each() is not an array or object in c:\program files\apache group\apache\htdocs\golf\dump.include on line 7
The count var : 0
I've tried this in several different ways, added debugging output to make sure what I thought should be happening was happening. It's like the array of objects and the count variables are not tied to the object life cycle ..... any insights?
Apologies for the formatting ...