PHP 嵌套对象函数调用不起作用


php nested objects function call not working?

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

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

但是我做了一个外部函数来孵化对象,但我喜欢这样称呼它。我知道我可以调用类var_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) {
  }
}

Fluffeh 是对的,如果它实际上是对该类的引用(我非常怀疑它是(,您的代码应该可以正常工作。下面是一个示例,模拟您的类可能是什么样子(具有相同的函数和数组(。

<?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

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

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

?>  

其中输出:

0
1

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

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

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

编辑:假设在我制作的第二个实例中还有其他arby实例,您可以从中调用函数,如下所示:

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

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