I got a doubt while doing this:
class Logger {
public static $log_INFO = 'INFO';
public static $log_ERROR = 'ERROR';
public function log($logLevel, $param2, $param3) {
// Write log to some file
}
}
class Midea {
public function fn1 {
$logger = new Logger();
$logger->log(Logger::$log_INFO, 'some', 'some2');
}
}
Now my question is: Is there any way to make the log function in Logger class to accept only the static variables (any static variable) of Logger class? It should not accept any other string or integers as arguments.
My answer was based on the fact that $logLevel contains the name of a static class property.
If you use it as the updated example Logger::$INFO, that will pass the value string(4) “INFO” and this will not work. it needs to pass the value string(8) “log_INFO”
Yes, by using reflection:
IMO this kind of enforcement is unnecessary, it adds both complexity and overhead to the code. And the benefits are small.
Coding your necessity like this seams more appropriate to me:
The structure above opens up a neat opportunity: