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) {
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With