Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
迭代器内的PHP调用方法_Php_Methods_Iterator - Fatal编程技术网

迭代器内的PHP调用方法

迭代器内的PHP调用方法,php,methods,iterator,Php,Methods,Iterator,我是面向对象的初学者,我想知道是否可以使用一些cast、magic方法或面向对象工程来调用下面的注释代码中的内容 提前谢谢 体育硕士 就这样打电话吧: $a->getNumber(); 这里,$a是myArrayObj类的对象。您可以根据类的访问修饰符从类外调用任何属性和方法 完整代码: <?php Class myArrayObj { public $myArray ; function __construct(){ $this->myA

我是面向对象的初学者,我想知道是否可以使用一些cast、magic方法或面向对象工程来调用下面的注释代码中的内容

提前谢谢 体育硕士

就这样打电话吧:

$a->getNumber();
这里,
$a
myArrayObj
类的对象。您可以根据类的
访问修饰符
从类外调用任何
属性
方法

完整代码:

<?php
Class myArrayObj  {
    public $myArray ;

    function __construct(){
        $this->myArray = array( 
    0 => 'a', 
    1 => array('subA','subB',array(0 => 'subsubA', 1 => 'subsubB', 2 => array(0 => 'deepA', 1 => 'deepB'))), 
    2 => 'b', 
    3 => array('subA','subB','subC'), 
    4 => 'c'
    );
    }

    function getNumber() {
        return count($this->myArray);
    }

    function countArray($ar) {
        return count($ar);
    }
}

$a = new myArrayObj();

$i = new RecursiveIteratorIterator(new RecursiveArrayIterator($a),RecursiveIteratorIterator::SELF_FIRST);

foreach($i as $key => $value) {
$type = gettype($value);
$depth = $i->getDepth();

if($i->hasChildren()) {
        echo "$depth: $key ($type) has children<br>";
        /* here is it possible to call ....->getNumber(); */
        echo $a->countArray($value);
    } 
    else {
        echo "$depth: $key ($type) has no children<br>";
        /* here is it possible to call ....->getNumber(); */
        echo $a->countArray($value);
    }
}

您可以简单地执行
$a->getNumber()
,但我觉得我没有正确理解这里的问题。“是否可以使用一些强制转换、魔术方法或面向对象的工程来调用下面注释代码中的内容?”-不。因为与原始对象的关系已断开
RecursiveArrayIterator
将对象强制转换为数组。您好,我的目的是通过$i RecursiveIterator调用->getName()。对于主数组中的每个数组派生程序,获取count元素。。。不总是主数组的coutn。。。现在更清楚了?是的,你可以。只要在你需要的地方调用这个方法,你能实现吗?或者你想从我这里得到什么?首先你自己试试。我昨天花了一些时间,我尝试了$I->getName(),但当然这是不可能的,因为递归迭代器和myArrayObj之间没有任何关系,我尝试用递归迭代器扩展myArrayObj,但没有结果。。。我得做些石膏?
<?php
Class myArrayObj  {
    public $myArray ;

    function __construct(){
        $this->myArray = array( 
    0 => 'a', 
    1 => array('subA','subB',array(0 => 'subsubA', 1 => 'subsubB', 2 => array(0 => 'deepA', 1 => 'deepB'))), 
    2 => 'b', 
    3 => array('subA','subB','subC'), 
    4 => 'c'
    );
    }

    function getNumber() {
        return count($this->myArray);
    }

    function countArray($ar) {
        return count($ar);
    }
}

$a = new myArrayObj();

$i = new RecursiveIteratorIterator(new RecursiveArrayIterator($a),RecursiveIteratorIterator::SELF_FIRST);

foreach($i as $key => $value) {
$type = gettype($value);
$depth = $i->getDepth();

if($i->hasChildren()) {
        echo "$depth: $key ($type) has children<br>";
        /* here is it possible to call ....->getNumber(); */
        echo $a->countArray($value);
    } 
    else {
        echo "$depth: $key ($type) has no children<br>";
        /* here is it possible to call ....->getNumber(); */
        echo $a->countArray($value);
    }
}