0

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[]

Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120

2 Answers2

2

Change the following line to. This ensures that $type is evaluated into 'message' or 'error' first.

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

To expand on this, this is the code that I have. There seems to be additional syntax errors in your code that is causing the other failures, but this is why $$type[] is giving you that error.

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);

And this is the output that I get

Array
(
    [0] => Regular Message
)
Array
(
    [0] => Error Message
)
David Z.
  • 5,621
  • 2
  • 20
  • 13
  • Can you elaborate on what is not working. I'm trying this out locally and it seems to work for me. – David Z. Apr 25 '12 at 00:51
  • I am getting a fatal error. My if(!in_array($type ... ) test should catch bad values in the method argument $type. The problem appears to be in the self::$$type[] assignment (as per the Fatal Error I mention) – Buttle Butkus Apr 25 '12 at 00:55
  • Please see revisions, there seems to be a few additional syntax issues in addition to the error in using `$$type[]`. I dumped out my code for you to try. – David Z. Apr 25 '12 at 01:00
  • Hey, I was surprised when your suggestion didn't work. It turned out my save was not uploaded due to network error. Now it does indeed work. Thank you for your answer and code clarification. I also added a recursive call to debug::add() to handle bad $type arguments. – Buttle Butkus Apr 25 '12 at 01:02
2

2 Errors

A. if(!in_array($type,self::$types) ) { not properly closed .. you used ) insted of } at the end

B. self::$all[] = $text; $text not defined anywhere in the script

Try

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"));
Baba
  • 94,024
  • 28
  • 166
  • 217
  • Both your listed errors A. and B. had nothing to do with the error I specified. A. was due to a typo (fixed) and B. was due to my code simplification for the post (fixed). Thanks for reading so closely. Also I like your get function and its return logic. Very nice. But the the other poster found the real error so I'll give you +1 but I'm giving him best answer. – Buttle Butkus Apr 25 '12 at 01:09
  • I saw that too just did not think saving $message twice is effective and efficient – Baba Apr 25 '12 at 01:31