PHP function: find argument's variable name, and function calls line number

Posted by Majid on Stack Overflow See other posts from Stack Overflow or by Majid
Published on 2010-05-20T18:30:47Z Indexed on 2010/05/20 18:40 UTC
Read the original article Hit count: 139

Filed under:

I want to do something like this for simplifying logging operations. Any idea what I should put in for ?[1]? and ?[2]??

function log_var($var) {
  $line = ?[1]?;
  $var_name = ?[2]?;

  $line--;

  $filepath = 'log-' . date('Y-m-d'). '.txt';
  $message  = "$line, $var_name = $var\n";
  $fp = fopen($filepath, "a");
  fwrite($fp, $message);
  fclose($fp);
  @chmod($filepath, 0666);
  return TRUE;
}

This how I'd use the function in code (numbers are assumed to be line numbers in actual code):

23  $a = 'hello';
24  log_var($a);
25  $b = 'bye';
26  log_var($b);

And this is what I want to be written to the log file:

23, a = hello
25, b = bye

© Stack Overflow or respective owner

Related posts about php