2

I am trying to implement a logging library which would fetch the current debug level from the environment the application runs in:

23    $level = $_SERVER['DEBUG_LEVEL'];
24    $handler = new StreamHandler('/var/log/php/php.log', Logger::${$level});

When I do this, the code fails with the error:

A valid variable name starts with a letter or underscore,followed by any number of letters, numbers, or underscores at line 24.

How would I use a specific Logger:: level in this way?

UPDATE:

I have tried having $level = "INFO" and changing ${$level} to $$level. None of these changes helped.

However, replacing the line 24 with $handler = new StreamHandler('/var/log/php/php.log', Logger::INFO); and the code compiles and runs as expected.

The variable itself is declared here

PHP Version => 5.6.99-hhvm

user3081519
  • 2,659
  • 5
  • 25
  • 35
  • I would assume that error means `$level` contains a number or other invalid character? If your `$level` contains what would be a valid PHP variable name, you may need a to remove the `{}` so you get `Logger::$$level`. – Mr Glass Jul 03 '18 at 17:50
  • This works just fine, so I'd assume $level is invalid like Mr Glass said. https://3v4l.org/6Fo5o I usually represent variable variables as `$$level` though. – Devon Bessemer Jul 03 '18 at 17:55
  • 1
    As @MrGlass points out, `$level` probably contains an invalid character. Keep in mind that this error will also be thrown for an invalid class property name, variable may be a bit confusing in this context. – Niellles Jul 03 '18 at 17:56
  • @MrGlass even if I change the variable to be $level = "INFO", the error still remains. Also tried using $$level instead. Same result. – user3081519 Jul 03 '18 at 17:57
  • how you declare your static class var - can you please add the code? – Reflective Jul 03 '18 at 18:02
  • @Reflective I think it is declared here: https://github.com/Seldaek/monolog/blob/e5900c38147d13f0aa9072aaff7ff0f8dbb253f3/src/Monolog/Logger.php#L97 – user3081519 Jul 03 '18 at 18:06
  • the only related static var I see is `protected static $levels` so if you want to get this var write exactly `Logger::$levels` which will return the value of this var – Reflective Jul 03 '18 at 18:14
  • @Reflective pardon, I looked at the wrong line. I guess the interface implements ```INFO``` as a constant here: https://github.com/Seldaek/monolog/blob/e5900c38147d13f0aa9072aaff7ff0f8dbb253f3/src/Monolog/Logger.php#L40 So I, in fact want to get to that constant somehow. If I do $levels it will give me the whole array which is not the intention. – user3081519 Jul 03 '18 at 18:17
  • ok, now I see what you want to achieve. I'm not sure and not tried but may be `Logger::constant($level)` may help. Of course you can always do the following `$logger_levels = Logger::$levels; $reversed_logger_levels = array_flip($logger_levels); $l = $reversed_logger_levels[$level];` – Reflective Jul 03 '18 at 18:30
  • @Reflective. Ok, the 2nd option looks pretty ugly, but you are right, there is a ```constant()``` function which does what I want. – user3081519 Jul 03 '18 at 18:45

2 Answers2

2

So the answer was to use a function for a constant lookup:

$handler = new StreamHandler('/var/log/php/php.log', constant("Monolog\Logger::" . $level));

user3081519
  • 2,659
  • 5
  • 25
  • 35
0
<?php
class Logger {
  const MY = 1;
}

$lookingfor = 'MY';

// approach 1
$value1 = (new ReflectionClass('Logger'))->getConstants()[$lookingfor];
// approach 2
$value2 = constant("Logger::" . $lookingfor);

echo "$value1|$value2";
?>

Result: "1|1"

Reflective
  • 3,854
  • 1
  • 13
  • 25
  • :shrug: definitely prefer the 2nd option for the reasons of being a language built in, having more than 0 documentation and simplicity. I think most people would prefer to avoid a reflection wherever possible. So while containing variable is 1:1, the code functionally and structurally is not equivalent. – user3081519 Jul 03 '18 at 19:26
  • Yep, but it's difficult to avoid errors on missing constants. – Reflective Jul 03 '18 at 19:42
  • that's why I'll be wrapping it into an exception handler. – user3081519 Jul 03 '18 at 20:45