2012-04-25 74 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