php-debug is a simple debugging class for use in PHP scripts. Use it when you don't have access to a debugger, like in a customer's environment, or you need to gather complex and/or larger amounts of data that are not feasible to analyze in a debugging session.
Features
Encapsulate debugging values in a single variable, making it much easier to identify and remove debugging code later.
Save complex data structures, utilizing arrays.
Keep only a specific number of log entries (the most recent, defaults to 500) to avoid filling up disk space.
Log data in JSON format.
Support
This library has not been tested on PHP versions prior to 5.3.29. If you try it, please let us know how it goes by opening a new issue.
If you find problems on any other versions, please also let us know in a new issue.
Usage
To use php-debug, simply drop the Debug.php file somewhere in your source files and include or require it in the file that needs to be debugged. Then, create a new Debug object and use it to gather, save, and log data.
When logging, data is stored in as JSON at your file location of choice (by default, debug.json in the same directory as Debug.php).
// Save data to the log file.$debug->log_data('hello');
$debug->log_data('counter');
API
The following API documentation is sorted alphabetically. To learn the basics and get started quickly, we recommend reading __construct, set, get, merge, log_data, and log_all.
All the parameters of the constructor have default values, so none are necessary to get started with Debug. The $data parameter sets the internal data store and should always be an associative array. The behavior of Debug is undefined when $data is set to a scalar or an object.
Parameters
Parameter
Type
Required
Default
Notes
$filename
string
'debug.json'
The default file is created in the same directory as Debug.php.
$data
array
array()
Use this to initialize the saved data.
$log_now
boolean
false
Log any initialized data immediately.
$max_entries
integer
500
The log file will be limited to this many entries (not lines).
Write data directly to the log file without saving it as a data element.
log_all and log_data interact with saved data elements, but this function skips that step for cases when you just want to log current values. log_all and log_data use this function internally.
Parameters
Parameter
Type
Required
Default
Notes
$data
mixed
✔️
The data element to log (can be a single value or multiple values in an integer-indexed array )
$raw
boolean
true
Whether or not to treat $data as a single value and log it as-is
When this is false, $data is treated as if it had been saved with set(). When this is true, you cannot save multiple values in $data.
Merge the $data array into the data element stored at $key. This is an easy way to update multiple values in a data element at once. Assumes $key references an array.