Tracking of PHP errors and warnings

Sometimes you need to keep track of non-fatal errors in PHP script (E_WARNING, E_NOTICE, etc.) and somewhere keep them.
For example, we want to validate the XML file using XMLReader (by the way, about this, I think I can tell in a separate publication). If an XML is invalid, then we throw an exception, halting further work.
All validation errors are issued through a WARNING-message.
To obtain such message can proceed as follows:

// Track the errors (to be able to store the PHP error message)
$oldConfigValue = ini_set('track_errors', '1');
while ($xmlReader->read()) {} // полностью читаем XML
if (!$xmlReader->isValid())
  throw new Exception('XML does not conform to schema. ' . $php_errormsg);
// Set back track_errors setting:
ini_set('track_errors', $oldConfigValue);

Here, we first establish the PHP directive 'track_errors' (Tracking Error) to TRUE (1). And then – magic! 🙂
There turns out to be such a wonderful built-in variable $php_errormsg , which contains and error messages (warnign, notice, etc.)
When this contraption we no longer need, we can return to the previous directive in place, because retained its previous value in a variable $oldConfigValue.
Thus we have a meaningful message about why our XML is not validated by our own scheme.