局部变量保持未初始化

问题描述:

私有阵列$list_of_files保持未初始化状态。我如何从while循环更新它?局部变量保持未初始化

class listOfFiles { 
private $list_of_files = []; 

function __construct() { 
    if ($handle = opendir(WEB_STORAGE_DIR)) { 

    while (false !== ($entry = readdir($handle))) { 
     $this->list_of_files[$entry] = filesize(WEB_STORAGE_DIR.DIRECTORY_SEPARATOR.$entry); 
    } 

    closedir($handle); 

    // Remove . and .. from the list 
    unset($list_of_files['.']); 
    unset($list_of_files['..']); 
    } 
} 

function is_empty() { 
    return empty($list_of_files); 
} 
} 

$list_of_files是指的是一个与$this->list_of_files属性不同的变量。

变量声明/在一个函数引用是仅在该功能有效(除非你使用全球 - 不过这被普遍认为是“邪恶”的,应该避免)

属性都可以从类中的所有方法(除非它们是静态的)并坚持物体的生命。

<?php 
//lets show all error so we can see if anything else is going on.. 
error_reporting(E_ALL & ~E_NOTICE); 

class listOfFiles { 
    private $list_of_files = []; 

    function __construct() { 
     if ($handle = opendir(WEB_STORAGE_DIR)) { 

     while (false !== ($entry = readdir($handle))) { 
      $this->list_of_files[$entry] = filesize(WEB_STORAGE_DIR.DIRECTORY_SEPARATOR.$entry); 
     } 

     closedir($handle); 

     // Remove . and .. from the list 
     unset($this->list_of_files['.']); 
     unset($this->list_of_files['..']); 
     } 
    } 

    function is_empty() { 
     return empty($this->list_of_files); 
    } 
} 

问题是目录不存在?这将是更好试图打开前检查这一点,并且还允许当它存在什么样的事,但你不能真正阅读:

<?php 
//lets show all error so we can see if anything else is going on.. 
error_reporting(E_ALL & ~E_NOTICE); 

class listOfFiles { 
    private $list_of_files = []; 

    function __construct() { 
     if(!is_dir(WEB_STORAGE_DIR)){ 
     throw new Exception("Missing Web Storage Directory"); 
     } 
     $handle = opendir(WEB_STORAGE_DIR); 
     if (!$handle) { 
     throw new Exception("Could not read Web Storage Directory"); 
     } 
     else{ 

     while (false !== ($entry = readdir($handle))) { 
      $this->list_of_files[$entry] = filesize(WEB_STORAGE_DIR.DIRECTORY_SEPARATOR.$entry); 
     } 

     closedir($handle); 

     // Remove . and .. from the list 
     unset($this->list_of_files['.']); 
     unset($this->list_of_files['..']); 
     } 
    } 

    function is_empty() { 
     return empty($this->list_of_files); 
    } 
} 

我已经加入error_reporting(E_ALL & ~E_NOTICE);的例子,因为这将确保您会看到任何错误并可能有助于调试您的问题。更多的信息在这里:http://php.net/manual/en/function.error-reporting.php

+0

谢谢,设置'$ this-> list_of_files [$条目]'在while循环失败,私有数组声明为对象的属性保持未初始化。 – Ralph

+0

您确定路径有效 - WEB_STORAGE_DIR来自哪里?我已经通过使用'__DIR__'来测试,而不是指向正在执行的文件的目录,它的工作原理:请参阅这里的示例代码http://pastebin.com/gAAzZSr6 – Theo

+0

你说它在while循环中失败 - 你会得到任何错误或警告?尝试添加'error_reporting(E_ALL&〜E_NOTICE);'到你的文件的顶部,以确保你看到错误 - 更多信息在这里:http://php.net/manual/en/function.error-reporting.php – Theo

访问一个属性,你需要使用$this,否则你正在创建一个局部变量。你在一个地方这样做,但例如不在这里

return empty($list_of_files); 

由于该变量从未设置,这将始终返回相同的东西。

return empty($this->list_of_files); 

这同样适用于其他财产的引用,使得完整的代码(这是未经测试,当然,因为你没有提供任何可检验)是这个样子

class listOfFiles { 
    private $list_of_files = []; 

    function __construct() { 
    if ($handle = opendir(WEB_STORAGE_DIR)) { 

     while (false !== ($entry = readdir($handle))) { 
     $this->list_of_files[$entry] = filesize(WEB_STORAGE_DIR.DIRECTORY_SEPARATOR.$entry); 
     } 

     closedir($handle); 

     // Remove . and .. from the list 
     unset($this->list_of_files['.']); 
     unset($this->list_of_files['..']); 
    } 
    } 

    function is_empty() { 
    return empty($this->list_of_files); 
    } 
} 
+0

谢谢,我明白了,我在return语句中缺少'$ this - > ...'。我不清楚我的问题;在while循环中'$ this-> list_of_files [$ entry]'的设置失败,被声明为对象属性的私有数组保持未初始化状态。 – Ralph

+0

我不知道你的意思,或者你如何测试这个。你应该看看一些基本的调试,然后确切地说出了什么问题,以及错误是什么。无论如何,首先修复你的代码的属性,ID'说 – Nanne