How to get your Error reporting constant values

PHP lets you specify the Error Reporting level, so that when a piece of your code is run based on this setting of the php.ini file the generated errors and/or warnings will be shown or not. Generally speaking in a development environment it is recommended to set this as high as possible so that you are informed about all the little details of your code whereas in  a production environment this setting is usually set to something less “explanatory”.

In order to set the value of the error_reporting directive php constant values and bitwise operators are used, as they are described in the PHP manual . The setting can be set during runtime using the php error_reporting() function (manual) passing as a parameter the int value calculated based on the constant values and bitwise operators used. The function returns the integer value of the previous error_reporting level.

Now if you wish to find out what is your current error_reporting level you could go and have a look in your php.ini file or you could use the error_reporting() function with no parameters, in which case the function will return an integer value representing the current error reporting level. If you go via the php.ini file you will see the names of the constants used and the operations applied to produce your current reporting level but if you try to get a similar result via a php file that you have written you might notice that it is more difficult to understand  what is going on since now you are only dealing with an integer value and not with the named constants. So which is the description of your error reporting level and how can you retrieve it from within a php file?

In order to be able to retrieve the error reporting level we will create a class named ErrorReporting with two protected properties $level and $levels which will hold the current integer value of the error reporting level that we have in our system and the second will be an array of the php constants along with their integer value. When a new object is constructed the current integer value of the error reporting level is stored in the $level variable. In addition we will need a public function that will return the error reporting level as a string representation of the constants. For this function to return a result we should first find out which bits are turned on and then present them in a nice looking way.

So our class looks like this so far

'E_ERROR',
2 => 'E_WARNING',
4 => 'E_PARSE',
8 => 'E_NOTICE',
16 => 'E_CORE_ERROR',
32 => 'E_CORE_WARNING',
64 => 'E_COMPILE_ERROR',
128 => 'E_COMPILE_WARNING',
256 => 'E_USER_ERROR',
512 => 'E_USER_WARNING',
1024 => 'E_USER_NOTICE',
2048 => 'E_STRICT',
4096 => 'E_RECOVERABLE_ERROR',
8192 => 'E_DEPRECATED',
16384 => 'E_USER_DEPRECATED'
);

protected $level;

public function __construct()
{
$this->level = error_reporting();
}

public function getErrorLevel()
{
$included = $this->_getIncluded();

$errorLevel = $this->_getErrorDescription($included);

return $errorLevel;
}

}

The _getIncluded() and _getErrorDescription() functions are protected helper functions that will assist us in our task. Let’s have a look at them. The _getIncluded() will return the bits that are set or to put it in another way, from the list of all possible errors and warnings it will return the ones that we have chosen to be informed about. The function returns an array with the integer values of the constants that are used in our error reporting.

The _getErrorDescription() function is also a helper protected function that takes as input the constants included and returns the string representation of the error reporting level. A word on why it is a good idea to use this function. We have a total of 15 levels (16 if you count E_ALL as well) to choose from when defining our error reporting level. This means that there might be cases when we have included many of the levels and others when we only included a few. It would be nice to change the string representation based on the number of values we wish to report on. So if we have used many of the levels it would be nice to use E_ALL and exclude the few that we did not use or if we just used a few values then it would be nice to simply list the values that we have used.

So let’s have a look at the whole class now

'E_ERROR',
2 => 'E_WARNING',
4 => 'E_PARSE',
8 => 'E_NOTICE',
16 => 'E_CORE_ERROR',
32 => 'E_CORE_WARNING',
64 => 'E_COMPILE_ERROR',
128 => 'E_COMPILE_WARNING',
256 => 'E_USER_ERROR',
512 => 'E_USER_WARNING',
1024 => 'E_USER_NOTICE',
2048 => 'E_STRICT',
4096 => 'E_RECOVERABLE_ERROR',
8192 => 'E_DEPRECATED',
16384 => 'E_USER_DEPRECATED'
);

protected $level;

public function __construct($level)
{
$this->level = $level;
}

public function getErrorLevel()
{
$included = $this->_getIncluded();

$errorLevel = $this->_getErrorDescription($included);

return $errorLevel;
}

public function _getIncluded()
{
$included = array();

foreach($this->levels as $levelInt => $levelText)
{
// This is where we check if a level was used or not
if($this->level & $levelInt)
{
$included[] = $levelInt;
}
}

return $included;
}

protected function _getErrorDescription($included)
{
$description = '';

$all = count($this->levels);

$values = array();
if(count($included) > $all / 2)
{
$values[] = 'E_ALL';

foreach($this->levels as $levelInt => $levelText)
{
if(!in_array($levelInt, $included))
{
$values[] = $levelText;
}
}
$description = implode(' & ~', $values);
}
else
{
foreach($included as $levelInt)
{
$values[] = $this->levels[$levelInt];
}
$description = implode(' | ', $values);
}

return $description;
}
}

If we wish to use the class to get the string representation of the current error reporting level then we can instantiate a new object and call on its getErrorLevel() public function which will return the desired string. I have been running this php file command line hence the PHP_EOL you might notice in the echo statement.

$error = new ErrorReporting();

echo PHP_EOL . $error->getErrorLevel() . PHP_EOL . PHP_EOL;

Notes:
This is just some sample code and it is provided AS IS with no warranty what so ever just to demonstrate a possible way to get the string representation of the current error reporting level set on a system.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s