I am trying to change the value of a variable inside a class from one of its functions rather than the constructor, but it doesn't seem to work.

This is the code I am havinng difficulties with:

class Level
{
var $myChildrenNum=0;

function addChild($childObject)
{
$this->myChildrenNum = 2;
}

function countMyChildren()
{
return (count($this->myChildren));
}

}

The function countMyChildren() returns 0, although addChild() has been executed.
I am assuming there is some rule in PHP that I am not allowed to change the values of my variables from outside of the constructor, but there must be some way of doing this, hey?

    function countMyChildren()
    {
    return (count($this->myChildren));
    }

    is it suposed to be $this->myChildrenNum ????

      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();?>

        The problem lies with this piece of code...


        $parentOne = new Level('This is my content', 'java script: edit()', 'java script: add()', 'java script: delete()');
        $myArea->addLevel($parentOne);
        //create a child
        $childOne = new Level('I am a child', 'java script: edit()', 'java script: add()', 'java script: delete()');
        $parentOne->addChild($childOne);

        You are adding $parentone to $myarea before adding the child

        change it to ...


        $parentOne = new Level('This is my content', 'java script: edit()', 'java script: add()', 'java script: delete()');

        //create a child
        $childOne = new Level('I am a child', 'java script: edit()', 'java script: add()', 'java script: delete()');
        $parentOne->addChild($childOne);

        $myArea->addLevel($parentOne);

        and it should work!

          Write a Reply...