Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php nested objects function call not working?

I have a PHP class that can contain an array of its own type this class has also a function called hatch() when I call it for one of the array items it gives me a message you can't call the function. I call it like that

$this->arrayLikeThis[$i]->hatch();

however I made an outer function to hatch the object but I love to call it that way. I know that I can call any function of the the var_dump() of the class is like that:

object(web\ui\structure\tag)#1 (4) {
  ["name"]=>
  string(4) "meta"
  ["type"]=>
  int(1)
  ["attributes"]=>
  array(2) {
    [0]=>
    object(web\ui\structure\attribute)#2 (2) {
      ["attributeName"]=>
      string(7) "content"
      ["attributeValue"]=>
      string(24) "text/html; charset=utf-8"
    }
    [1]=>
    object(web\ui\structure\attribute)#3 (2) {
      ["attributeName"]=>
      string(10) "http-equiv"
      ["attributeValue"]=>
      string(12) "Content-Type"
    }
  }
  ["childNodes"]=>
  array(0) {
  }
}
like image 407
Romany Saad Avatar asked Dec 29 '25 06:12

Romany Saad


1 Answers

Fluffeh is right, your code should work fine if it's actually a reference to the class (which I highly doubt that it is). Here's an example that simulates what your class might look like (with the same functions and in an array).

<?php
    class egg {
        private $identifier;
        private $eggChildren = array();
        public function __construct($identifier) {
            $this->identifier = $identifier;
        }
        public function createEgg($identifier) {
            $this->eggChildren[$identifier] = new egg($this->identifier . " - " . $identifier);
        }
        public function hatch() {
            echo "Just hatched egg with ID " . $this->identifier . "<br />";
        }
        public function hatchAllChildren() {
            foreach ($this->eggChildren as $childID => $eggChild) {
                $eggChild->hatch();
            }
        }
    }
    class eggs {
        private $arrayLikeThis;
        const COUNT_EGGS = 3;
        const COUNT_EGG_CHILDREN = 3;
        public function createEggs() {
            $this->arrayLikeThis = array();
            for ($i = 0; $i < self::COUNT_EGGS; $i++) {
                $this->arrayLikeThis[$i] = new egg($i);
                for ($j = 0; $j < self::COUNT_EGG_CHILDREN; $j++) { 
                    $this->arrayLikeThis[$i]->createEgg($j);
                }
            }
        }
        public function hatchEggs() {
            for ($i = 0; $i < self::COUNT_EGGS; $i++) {
                $this->arrayLikeThis[$i]->hatchAllChildren();
                $this->arrayLikeThis[$i]->hatch();
            }
        }
    }

    $eggController = new eggs();
    $eggController->createEggs();
    $eggController->hatchEggs();
?>

And this will output

Just hatched egg with ID 0 - 0
Just hatched egg with ID 0 - 1
Just hatched egg with ID 0 - 2
Just hatched egg with ID 0
Just hatched egg with ID 1 - 0
Just hatched egg with ID 1 - 1
Just hatched egg with ID 1 - 2
Just hatched egg with ID 1
Just hatched egg with ID 2 - 0
Just hatched egg with ID 2 - 1
Just hatched egg with ID 2 - 2
Just hatched egg with ID 2
like image 140
h2ooooooo Avatar answered Dec 31 '25 19:12

h2ooooooo