Oops, sorry, when I shortened the code I made that mistake. In fact my code looks slightly different, yet it still doesn't work. The problem seems to be that I am trying to call the countMyChildren() function of an object in an array of objects. If I call the function of one specific object, it works.
Bit hard to explain, here comes the exact code with all the functions I use (sorry, didn't want to make it this long, but I can't see the mistake). Note the two lines of code I mark. One of them prints the correct result, the other doesn't.
<?php
/########################################################################
CLASS: DropArea
Responsibilities:
#######################################################################/
class DropArea
{
//variables
var $allPrimaryLevels; //holds an array of all primary levels
var $counter;
//add a level to the drop area
function addLevel($levelObject)
{
//add object to array
$this->allPrimaryLevels[]=$levelObject;
}
//prints the entire drop area
function printLevel()
{
//loop through all parent levels and print them
$counter = 1;
for ($i=0;$i<count($this->allPrimaryLevels);$i++)
{
//show-function and content
$content = $this->allPrimaryLevels[$i]->getMyContent();
echo("$content\n");
echo("<br>\n");
echo("<!-- here comes the children -->\n");
//if parent has children, print all of them as well
print($this->allPrimaryLevels[$i]->countMyChildren()); //<--------- this is the line that returns 0
echo("<!-- end the children -->\n");
}
}
}
/########################################################################
CLASS: Level
Responsibilities:
#######################################################################/
class Level
{
//variables
var $editable; //if true, display edit button
var $addable; //if true, display add button
var $deletable; //if true, display delete button
var $parentID; //if parent, holds unique ID of parent
var $editFunction; //code to put into href of edit-button
var $addFunction; //code to put into href of add-button
var $deleteFunction; //code to put into href of delete-button
var $myContent; //holds the content of the object (eg title or text)
var $myChildren; //holds an array of the child objects
//constructor
function Level($content, $editFunction, $addFunction, $deleteFunction)
{
//assign values to the object variables
$this->myContent = $content;
$this->editFunction = $editFunction;
$this->deleteFunction = $deleteFunction;
$this->addFunction = $addFunction;
//set further values
if ($editFunction) $this->editable=true;
if ($addFunction) $this->addable=true;
if ($deleteFunction) $this->deletable = true;
}
//add a child object to the current Object
//this will automatically create an extendable button
function addChild($childObject)
{
//add object to array
$this->myChildren[]=$childObject;
print(count($this->myChildren));
}
function getMyContent(){
return ($this->myContent);
}
function countMyChildren()
{
return (count($this->myChildren));
}
}
//create the drop area
$myArea = new DropArea();
//create a parent
$parentOne = new Level('This is my content', 'javascript: edit()', 'javascript: add()', 'javascript: delete()');
$myArea->addLevel($parentOne);
//create a child
$childOne = new Level('I am a child', 'javascript: edit()', 'javascript: add()', 'javascript: delete()');
$parentOne->addChild($childOne);
//print drop area
print($parentOne->countMyChildren()); //<--------- this is the line that returns 1 (the correct result)
$myArea->printLevel();?>