Question

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) {
  }
}
Was it helpful?

Solution

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

OTHER TIPS

You aren't specifying the actual object, just the array of objects:

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

where identifier is either a number if your array is numeric, or the name of the element that contains the object whose function you want to call.

I snapped up this example for you just now to show you:

<?php  
    class arby
    {
        public $myID=0;
        public $ray=array();

        public function insertNew()
        {
            $this->ray[0] = new arby();
        }

        public function updateMe()
        {
            $this->myID='1';
        }

        public function displayMe()
        {
            echo $this->myID."\n";
        }

    }

    $var= new arby();
    $var->displayMe();
    $var->insertNew();
    $var->ray[0]->updateMe();
    $var->ray[0]->displayMe();


?>  

which outputs:

0
1

This covers off a class that can have instances of itself within it, adding these into an array and calling functions from within them and individual objects within the array of objects.

Edit: Assuming that the hatch() function is a function within the attribute object, this should work:

$varName->attributes[0]->hatch();

Edit: Assuming that there were further instances of arby within the second one that I made you can call the functions from them like this:

$var->ray[0]->ray[1]->hatch();

This assumes that the variable $var has another instance of the arby class in the array element 0 which in turn has another array and you want to call the function of the object in the element 1 this time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top