在方法1中设置后,属性在方法2中为null

时间:2022-09-06 20:25:14

hoping you can help me out with this question!

希望你能帮我解决这个问题!

Index.php

include_once 'files.class.php';
$file_object = new FileObject('resources');
$file_object->ReturnCurrentDirectoryList();

files.class.php

class FileObject{
public $directory_list;

    function __construct($current_directory_in){
        $this->directory_list = $this->BuildCurrentDirectoryList($current_directory_in);
    }

    function BuildCurrentDirectoryList($current_directory_in){
        $i = 0;
        $iterator = new DirectoryIterator($current_directory_in);

        foreach ($iterator as $fileinfo){
            if ($fileinfo->isDir()){
                $this->directory_list[$i]['pathname'] = $fileinfo->getPathname();
            }elseif($fileinfo->isFile()){
                $this->directory_list[$i]['filename'] = $fileinfo->getFilename();
            }
            $i++;
        }
    }

    function ReturnCurrentDirectoryList(){
        var_dump($this->directory_list);
    }
}

At the end of all this, what is returned is

在这一切结束时,返回的是

null

but what should be returned is

但应归还的是什么

array  0 =>  array 'pathname' => string 'resources\.',  1 => array 'pathname' => string 'resources\..', 2 =>  array 'pathname' => string 'resources\Images'

I'm somewhat new to classes/methods..

我对类/方法有些新意。

2 个解决方案

#1


3  

This is wrong:

这是错的:

$this->directory_list = $this->BuildCurrentDirectoryList($current_directory_in);

You assign to $this->directory_list but BuildCurrentDirectoryList does not return anything. The function have side-effects only, no return value.

您分配给$ this-> directory_list但BuildCurrentDirectoryList不返回任何内容。该功能仅具有副作用,无返回值。

Remove the assignment so the constructor looks like this and you should be good to go:

删除赋值,以便构造函数看起来像这样,你应该好好去:

$this->directory_list = array(); //I like to initialise arrays to the empty array
$this->BuildCurrentDirectoryList($current_directory_in);

#2


1  

In your constructor, you are assigning directory_list to the return of BuildCurrentDirectoryList, but you are not returning nothing in BuildCurrentDirectoryList, you are assigning directory_list directly in that method. At the end, BuildCurrentDirectoryList returns NULL. So, either return the directory_list, or else just don't assign it like this:

在构造函数中,您将directory_list分配给BuildCurrentDirectoryList的返回,但是您没有在BuildCurrentDirectoryList中返回任何内容,而是直接在该方法中分配directory_list。最后,BuildCurrentDirectoryList返回NULL。所以,要么返回directory_list,要么就是不要像这样分配它:

function __construct($current_directory_in){
    $this->BuildCurrentDirectoryList($current_directory_in);
}

#1


3  

This is wrong:

这是错的:

$this->directory_list = $this->BuildCurrentDirectoryList($current_directory_in);

You assign to $this->directory_list but BuildCurrentDirectoryList does not return anything. The function have side-effects only, no return value.

您分配给$ this-> directory_list但BuildCurrentDirectoryList不返回任何内容。该功能仅具有副作用,无返回值。

Remove the assignment so the constructor looks like this and you should be good to go:

删除赋值,以便构造函数看起来像这样,你应该好好去:

$this->directory_list = array(); //I like to initialise arrays to the empty array
$this->BuildCurrentDirectoryList($current_directory_in);

#2


1  

In your constructor, you are assigning directory_list to the return of BuildCurrentDirectoryList, but you are not returning nothing in BuildCurrentDirectoryList, you are assigning directory_list directly in that method. At the end, BuildCurrentDirectoryList returns NULL. So, either return the directory_list, or else just don't assign it like this:

在构造函数中,您将directory_list分配给BuildCurrentDirectoryList的返回,但是您没有在BuildCurrentDirectoryList中返回任何内容,而是直接在该方法中分配directory_list。最后,BuildCurrentDirectoryList返回NULL。所以,要么返回directory_list,要么就是不要像这样分配它:

function __construct($current_directory_in){
    $this->BuildCurrentDirectoryList($current_directory_in);
}