局部变量保持未初始化状态
local variable stays uninitialized
私有数组 $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);
}
}
访问一个属性,你需要使用$this
,否则你就是在做一个局部变量。您在一个地方执行此操作,但是例如不在这里
return empty($list_of_files);
由于从未设置该变量,因此这将始终return相同的事情。
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);
}
}
$list_of_files
指的是一个变量,它与 属性 不同,后者是 $this->list_of_files
.
函数中的变量 declared/referenced 仅在该函数中可用(除非您使用全局 - 但这通常被认为是 'evil' 并且应该避免)
属性可从 class 中的所有方法获得(除非它们是静态的)并在对象的生命周期内持续存在。
<?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
私有数组 $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);
}
}
访问一个属性,你需要使用$this
,否则你就是在做一个局部变量。您在一个地方执行此操作,但是例如不在这里
return empty($list_of_files);
由于从未设置该变量,因此这将始终return相同的事情。
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);
}
}
$list_of_files
指的是一个变量,它与 属性 不同,后者是 $this->list_of_files
.
函数中的变量 declared/referenced 仅在该函数中可用(除非您使用全局 - 但这通常被认为是 'evil' 并且应该避免)
属性可从 class 中的所有方法获得(除非它们是静态的)并在对象的生命周期内持续存在。
<?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