php嵌套对象函数调用不起作用?

php嵌套对象函数调用不起作用?,php,oop,class,inner-classes,function-call,Php,Oop,Class,Inner Classes,Function Call,我有一个PHP类,它可以包含自己类型的数组。这个类还有一个名为hatch()的函数,当我为其中一个数组项调用它时,它会给我一个消息,你不能调用这个函数。我这样称呼它 $this->arrayLikeThis[$i]->hatch(); 然而,我做了一个外部函数来填充对象,但我喜欢这样称呼它。我知道我可以调用类的var\u dump()的任何函数,如下所示: object(web\ui\structure\tag)#1 (4) { ["name"]=> string(4

我有一个PHP类,它可以包含自己类型的数组。这个类还有一个名为
hatch()
的函数,当我为其中一个数组项调用它时,它会给我一个消息,你不能调用这个函数。我这样称呼它

$this->arrayLikeThis[$i]->hatch();
然而,我做了一个外部函数来填充对象,但我喜欢这样称呼它。我知道我可以调用类的
var\u dump()
的任何函数,如下所示:

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) {
  }
}

您没有指定实际对象,只是指定对象数组:

$this->arrayLikeThis[identifier]->hatch();
其中
identifier
是数字(如果数组是数字),或者是包含要调用其函数的对象的元素的名称

我刚才为您抓拍了这个示例,向您展示:

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


?>  
这涵盖了一个类,该类可以在其中包含自身的实例,将这些实例添加到数组中,并从其中调用函数以及对象数组中的单个对象

编辑:假设
hatch()
函数是
属性
对象中的一个函数,这应该可以工作:

$varName->attributes[0]->hatch();
编辑:假设在我创建的第二个实例中还有更多的
arby
实例,您可以这样从它们调用函数:

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

这假设变量
$var
在数组元素
0
中有另一个
arby
类的实例,该类又有另一个数组,您希望这次调用元素
1
中对象的函数。

Fluffeh是正确的,如果代码实际上是对类的引用(我对此深表怀疑),那么代码应该可以正常工作。下面的示例模拟了类的外观(使用相同的函数和数组)


您的代码在哪里?确切的错误是什么?var_dump($this->arrayLikeThis[$i])的输出是什么?根据您发布的输出,我可以看出您只有在
数组中才有对象此['attributes']
数组是否也可以将代码发布到创建对象的起始位置?抱歉,我忘了将其放在问题中,我已经完成了,我将更新问题谢谢。@Romany1为了确保,
图案填充()
函数在
属性中
对象是-还是在
子节点中
对象中?如果是后者,那么您的var_转储不会显示使用
=new tag()创建的任何内容
这意味着对象未设置-因此不能调用其函数。@Romany1St我提供了一个代码示例,允许您在需要时调用第三级对象内的函数。这可能有助于你澄清。谢谢你,这是关于
foreach
循环而不是for
loop
$var->ray[0]->ray[1]->hatch();
<?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();
?>
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