2017-02-19 121 views
1

私有陣列$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); 
} 
} 

回答

0

$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

0

訪問一個屬性,你需要使用$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