Php 属性在方法1中设置后在方法2中为null
希望你能帮我解决这个问题 Index.phpPhp 属性在方法1中设置后在方法2中为null,php,methods,Php,Methods,希望你能帮我解决这个问题 Index.php include_once 'files.class.php'; $file_object = new FileObject('resources'); $file_object->ReturnCurrentDirectoryList(); class FileObject{ public $directory_list; function __construct($current_directory_in){ $th
include_once 'files.class.php';
$file_object = new FileObject('resources');
$file_object->ReturnCurrentDirectoryList();
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);
}
}
files.class.php
include_once 'files.class.php';
$file_object = new FileObject('resources');
$file_object->ReturnCurrentDirectoryList();
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);
}
}
在这一切结束时,返回的是
null
array 0 => array 'pathname' => string 'resources\.', 1 => array 'pathname' => string 'resources\..', 2 => array 'pathname' => string 'resources\Images'
但是应该返回的是
null
array 0 => array 'pathname' => string 'resources\.', 1 => array 'pathname' => string 'resources\..', 2 => array 'pathname' => string 'resources\Images'
我对类/方法有些陌生。这是错误的:
$this->directory_list = $this->BuildCurrentDirectoryList($current_directory_in);
您分配给$this->directory\u list
,但BuildCurrentDirectoryList
不返回任何内容。该函数只有副作用,没有返回值
删除赋值,使构造函数看起来像这样,您应该可以开始了:
$this->directory_list = array(); //I like to initialise arrays to the empty array
$this->BuildCurrentDirectoryList($current_directory_in);
在构造函数中,您将目录列表分配给BuildCurrentDirectoryList的返回,但在BuildCurrentDirectoryList中不返回任何内容,而是直接在该方法中分配目录列表。最后,BuildCurrentDirectoryList返回NULL。因此,要么返回目录列表,要么不这样分配它:
function __construct($current_directory_in){
$this->BuildCurrentDirectoryList($current_directory_in);
}
我一直在使用var_dump检查它,但是在
BuildCurrentDirectoryList()
的末尾,一切看起来都很好,然后在ReturnCurrentDirectoryList()中吐出一个null
。不知道为什么或者我可以做些什么来删除它。你可以用函数BuildCurrentDirectoryList
的返回覆盖你的$this->directory\u list
属性(在construct()中),该函数不返回任何内容。太好了!我看了以后觉得自己好笨。谢谢:)如果您在每个函数的顶部编写函数规范,并带有返回值和副作用,那么这些事情可能会更容易发现?太好了!我看了以后觉得自己好笨。谢谢:)