【问题标题】:PHP Error when using variable variable to insert data into static variable使用变量变量将数据插入静态变量时出现PHP错误
【发布时间】:2012-05-05 16:02:14
【问题描述】:

我不太擅长这个,所以我敢肯定这是一个愚蠢的问题。

我有一堂课:

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
  }

}

我从另一个类中调用它以进行调试(惊喜)。

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

来自 error.log 的 PHP 错误消息:

PHP 致命错误:无法使用 [] 在第 1248 行读取 /var/www/lib/lib.php

它指的是调试类中的上述指定行。

编辑:

我想要做的是使用变量变量(因此是发布标题)来确定要向哪个静态数组添加数据。

即如果 $type == 'messages',则 $$type == $messages。

所以我想要 self::$$type[] == self::$messages[]

或者如果 $type == 'errors',那么 $$type == $errors 和 self::$$type[] == self::$errors[]

【问题讨论】:

  • 您有错误数组,但您将其添加到错误数组中。
  • 在您的情况下 $types 是一个数组,索引 $types[] 未设置,但您正尝试将其用作变量。当然这是错误的。您能解释一下您打算实现的目标吗?
  • @tpaksu,说得好,但你看我的 if(!in_array(...)) 应该解决这个问题。
  • 是的,只是想说我看到了什么。

标签: php arrays class static-methods variable-variables


【解决方案1】:

将以下行更改为。这可确保首先将 $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
)

【讨论】:

  • 你能详细说明什么不起作用。我正在本地尝试这个,它似乎对我有用。
  • 我遇到了一个致命错误。我的 if(!in_array($type ... ) 测试应该在方法参数 $type 中捕获错误值。问题似乎出在 self::$$type[] 分配中(根据我提到的致命错误)
  • 请查看修订版,除了使用$$type[] 的错误之外,似乎还有一些额外的语法问题。我倾倒了我的代码供你尝试。
  • 嘿,当你的建议不起作用时,我很惊讶。原来我的存档由于网络错误没有上传。现在它确实有效。感谢您的回答和代码说明。我还添加了对 debug::add() 的递归调用来处理错误的 $type 参数。
【解决方案2】:

2 个错误

A. if(!in_array($type,self::$types) ) { 没有正确关闭.. 你最后使用了) insted of }

B. 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"));

【讨论】:

  • 您列出的错误 A. 和 B. 都与我指定的错误无关。 A. 是由于拼写错误(已修复)而 B. 是由于我对帖子的代码简化(已修复)。感谢您仔细阅读。我也喜欢你的 get 函数及其返回逻辑。很不错。但是另一位发帖人发现了真正的错误,所以我会给你+1,但我会给他最好的答案。
  • 我也看到了,只是觉得保存$message两次没有效果和效率
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 1970-01-01
  • 2011-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多