I'm not very good at this, so I'm sure this is a stupid question.
I have a class:
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
}
}
I'm calling this from another class in order to debug (Surprise).
debug::add('error', 'Error in ' . __FILE__ . ' on line ' . __LINE__);
PHP error message from error.log:
PHP Fatal error: Cannot use [] for reading in /var/www/lib/lib.php on line 1248
It refers to the above-specified line in the debug class.
EDIT:
What I am trying to do is use a variable variable (hence the posting title) to determine which static array to add data to.
I.e. if $type == 'messages', then $$type == $messages.
So I want self::$$type[] == self::$messages[]
Or if $type == 'errors', then $$type == $errors and self::$$type[] == self::$errors[]