【问题标题】:php nested objects function call not working?php嵌套对象函数调用不起作用?
【发布时间】:2012-08-28 20:49:46
【问题描述】:

我有一个 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) {
  }
}

【问题讨论】:

  • 您的代码在哪里,确切的错误是什么?
  • var_dump($this->arrayLikeThis[$i]); 的输出是什么?
  • 根据您发布的输出,我可以看出您仅在 arrayLikeThis['attributes'] 数组中拥有对象
  • 你能把你创建对象的代码贴出来吗?

标签: php oop class inner-classes function-call


【解决方案1】:

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

【讨论】:

    【解决方案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();
    

    编辑:假设在我创建的第二个实例中有 furtherarby 实例,您可以像这样调用它们的函数:

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

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

    【讨论】:

    • 抱歉,我忘了把它放在我已经完成的问题中,我会更新问题谢谢。
    • @Romany1St 只是为了确保 hatch() 函数在 attribute 对象中是 - 还是在 childNodes 对象中?如果是后者,那么您的 var_dump 不会显示使用 = new tag(); 创建的任何内容,这意味着该对象未设置 - 因此您不能调用它的函数。
    • @Romany1St 我包含了一个代码示例,如果需要,您可以在第三级对象中调用函数。这可能有助于为您解决问题。
    • 谢谢,这是关于foreach 循环而不是loop
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2017-06-01
    相关资源
    最近更新 更多