简体   繁体   English

如何在我的递归函数 php 中传递一个数组

[英]how to pass an array in my recursive function php

I have created a recursive function scanFile but I can't return an array with this function, It gives me an error when I want to push my $array[$key] in $this -> fileArray :我创建了一个递归函数scanFile但我不能用这个函数返回一个数组,当我想在$this -> fileArray推送我的$array[$key]时它给了我一个错误:

PHP Fatal error: Uncaught Error: Using $this when not in object context in createSprite.php:75 PHP 致命错误:未捕获错误:在 createSprite.php:75 中不在对象上下文中时使用 $this

class createSprite
{
    public $array;
    public $fileArray = array();

    function __construct($array)
    {
        global $argc;
        $this -> arr = $array;
        $this -> argc = $argc;
        $this -> fileArray = $fileArray;
    }

    public function recursiveGetImage()
    {
        if($this -> argc > 2)
        {       
                $fileArray = array();

                function scanFile($dir) 
                {
                    $array = glob($dir . '/*');

                    if (is_array($array)) 
                    {
                        foreach($array as $key => $file) 
                        {
                            if (is_dir($file)) 
                            {
                                scanFile($file);
                            }
                            elseif(is_file($file)) 
                            {
                               array_push($this -> fileArray, $array[$key]);
                            }
                        }                        

                    }
                }

                for($i = 1; $i < $this -> argc; $i++)
                {
                    if(is_dir($this -> arr[$i]))
                    {
                        var_dump(scanFile($this -> arr[$i]));
                    }
                }
        }
        else
        {
            echo "Entrez plusieurs fichiers";
        }
    } 

}

Don't use nested functions in classes - use another class method ( private if it is only used internally), this will stop the problem...不要在类中使用嵌套函数 - 使用另一个类方法(如果仅在内部使用,则为private ),这将阻止问题...

private function scanFile($dir) {
...
}

Then call it using然后调用它使用

$this->scanFile($file);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM