I’m trying to make a debugging class for one of my websites, something similar to the logger class in Java.
<?php
abstract class DebugLevel
{
const Notice = 1;
const Info = 2;
const Warning = 4;
const Fatal = 8;
}
class Debug
{
private static $level = DebugLevel::Fatal | DebugLevel::Warning | DebugLevel::Info | DebugLevel::Notice;
}
?>
I get a Parse error:
Parse error: syntax error, unexpected '|', expecting ',' or ';' in (script path) on line 13
What’s wrong?
You can’t add logic to a class property (variable) or constant in PHP.
From the documentation:
To set such a value use the
__construct()function.or maybe more elegant:
Then you can call this by: