【问题标题】:local variable stays uninitialized局部变量保持未初始化
【发布时间】:2017-02-19 11:42:55
【问题描述】:

私有数组$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);
}
}

【问题讨论】:

    标签: php-7.1


    【解决方案1】:

    $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 &amp; ~E_NOTICE); 添加到示例中,因为这将确保您看到任何错误并可能有助于调试您的问题。更多信息在这里:http://php.net/manual/en/function.error-reporting.php

    【讨论】:

    • 谢谢,在 while 循环中设置 $this-&gt;list_of_files[$entry] 失败,声明为对象属性的私有数组保持未初始化状态。
    • 您确定路径有效 - WEB_STORAGE_DIR 来自哪里?我已经使用__DIR__ 进行了测试,它指向正在执行的文件的目录并且它可以工作:请参阅此处的示例代码pastebin.com/gAAzZSr6
    • 你说它失败了,在 while 循环中 - 你得到任何错误或警告吗?尝试将error_reporting(E_ALL &amp; ~E_NOTICE); 添加到文件顶部以确保您看到错误 - 更多信息请点击此处:php.net/manual/en/function.error-reporting.php
    • WEB_STORAGE_DIR 来自一个包含的配置文件。您的 pastebin 代码也适用于我,所以我一定做错了其他事情。感谢 error_reporting 选项。我将用它来深入挖掘并找出问题所在。非常感谢。
    • @Ralph 很高兴听到;如果这已经回答了您的直接问题,您是否能够接受答案(我得到了一些互联网积分:) - 如果他们偶然发现相同的问题,它会帮助其他人)
    【解决方案2】:

    访问一个属性,你需要使用$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);
      }
    }
    

    【讨论】:

    • 谢谢,我明白了,我在退货声明中缺少$this-&gt;...。我的问题不清楚;在 while 循环中设置 $this-&gt;list_of_files[$entry] 失败,声明为对象属性的私有数组保持未初始化。
    • 我不知道你的意思是什么,或者你如何测试它。您可能应该研究一些基本的调试,然后准确地告诉您出了什么问题,以及错误是什么。无论如何,先用属性修复你的代码,id'说
    猜你喜欢
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2016-09-07
    相关资源
    最近更新 更多