2012-04-25 75 views
0

我不是很擅長這個,所以我確信這是一個愚蠢的問題。PHP使用變量變量將數據插入靜態變量時出錯

我有一個類:

class debug { 
    private static $messages = array(); 
    private static $errors = array(); 
    private static $all = array(); // includes both of above 
    private static $types = array('messages','errors'); 
    public static function add($type, $message) { 
    if(!in_array($type,self::$types)) { 
     self::add('errors','Bad type "' . $type . '" specified when sending this message: ' . $message); 
     return false; 
    } 
    self::$$type[] = $message; // ERROR IS HERE (see below) 
    self::$all[] = $message; // no error 
    } 

} 

我從另一個類,以便調用這個調試(驚喜)。從error.log中

debug::add('error', 'Error in ' . __FILE__ . ' on line ' . __LINE__); 

PHP錯誤消息:

PHP致命錯誤:無法使用[]爲上線1248

它指的是讀入/var/www/lib/lib.php調試類中的上述指定行。

編輯:

我所試圖做的是使用一個變量變量(因此發佈標題),以確定哪些靜態數組將數據添加到。

I.e.如果$ type =='messages',那麼$$ type == $ messages。

所以我想自己:: $$型[] ==自:: $信息[]

或者,如果$類型== '錯誤',那麼$$類型== $錯誤和自我:: $$ type [] == self :: $ errors []

+0

你有錯誤的數組,但你將它添加到錯誤數組。 – 2012-04-25 00:18:58

+0

在你的情況下,$ types是一個數組,索引$ types []沒有被設置,但你正在嘗試使用它作爲一個變量。當然這是錯誤的。你能解釋你打算實現什麼嗎? – shawndreck 2012-04-25 00:21:01

+0

@tpaksu,好點,但你看我的if(!in_array(...))應該照顧這個問題。 – 2012-04-25 00:50:43

回答

2

將以下行更改爲。這確保$type首先被評估爲'消息'或'錯誤'。

self::${$type}[] = $message; 

爲了擴大這一點,這是我的代碼。您的代碼中似乎還有其他語法錯誤導致其他錯誤,但這就是爲什麼$$type[]正在爲您提供該錯誤。

class debug { 
    public static $messages = array(); 
    public static $errors = array(); 
    public static $all = array(); // includes both of above 
    private static $types = array('messages','errors'); 
    public static function add($type, $message) { 
     self::${$type}[] = $message; 
     self::$all[] = $text; 
    } 
} 

debug::add('messages', "Regular Message"); 
debug::add('errors', "Error Message"); 

print_r(debug::$messages); 
print_r(debug::$errors); 

這是輸出,我得到

Array 
(
    [0] => Regular Message 
) 
Array 
(
    [0] => Error Message 
) 
+0

對不起,這沒有幫助。 – 2012-04-25 00:48:12

+0

你能詳細說明什麼是不工作的。我在本地嘗試這個,它似乎爲我工作。 – 2012-04-25 00:51:31

+0

我收到一個致命錯誤。我的if(!in_array($ type ...)測試應該在方法參數$ type中捕獲錯誤的值。這個問題似乎在self :: $$ type []賦值中(根據我提到的致命錯誤) – 2012-04-25 00:55:55

2

2錯誤

A. if(!in_array($type,self::$types)) {沒有正確關閉..你用) insted的的}

self::$all[] = $text;$text未在腳本的任何位置定義

嘗試

class Debug { 
    private static $errors = array(); 
    private static $types = array (
      'messages', 
      'errors' 
    ); 
    public static function add($type, $message) { 
     if (! in_array ($type, self::$types)) { 
      return false; 
     } 
     self::$errors [$type][] = $message; // results in error (see below) 
    } 

    public static function get($type = null) { 
     if (! in_array ($type, self::$types) && $type !== null) { 
      return false; 
     } 

     return ($type === null) ? self::$errors : self::$errors [$type] ; 
    } 
} 

debug::add ('errors', 'Error in ' . __FILE__ . ' on line ' . __LINE__); 
debug::add ('messages', 'Error in ' . __FILE__ . ' on line ' . __LINE__); 

var_dump(debug::get()); 
var_dump(debug::get("messages")); 
+0

您列出的錯誤A.和B.與我指定的錯誤無關。 A.是由於一個錯字(固定)和B.是由於我的代碼簡化(職位)(固定)。非常感謝您的閱讀。我也喜歡你的get函數和返回邏輯。非常好。但另一張海報發現真正的錯誤,所以我會給你+1,但我給他最好的答案。 – 2012-04-25 01:09:29

+0

我看到這樣也不覺得儲蓄$消息兩次是有效和高效的 – Baba 2012-04-25 01:31:55