Skip to content

Instantly share code, notes, and snippets.

@YavorK
Last active September 1, 2025 10:04
Show Gist options
  • Save YavorK/416cb36d93dbc69fe26881d004538eb9 to your computer and use it in GitHub Desktop.
Save YavorK/416cb36d93dbc69fe26881d004538eb9 to your computer and use it in GitHub Desktop.
<?php
// standalone implementation of functions similar to Laravel's dd(), dump() etc. functions. just copy and paste this code into any
// enviromnent's config.php or something
// and use dump() and dd() freely
if (!function_exists('dump')) {
/**
* Dump the given variables and continue execution
*/
function dump(...$vars) {
foreach ($vars as $var) {
echo '<div style="background: #f8f9fa; border: 1px solid #dee2e6; border-radius: 4px; padding: 15px; margin: 10px 0; font-family: Consolas, monospace; font-size: 13px; line-height: 1.4; color: #212529;">';
echo '<div style="color: #6c757d; font-size: 11px; margin-bottom: 8px;">';
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
echo htmlspecialchars($trace['file'] ?? 'unknown') . ':' . ($trace['line'] ?? 'unknown');
echo '</div>';
echo '<pre style="margin: 0; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, monospace; font-size: 13px; line-height: 1.4; ">';
echo htmlspecialchars(prettyPrint($var));
echo '</pre>';
echo '</div>';
}
}
}
if (!function_exists('dd')) {
/**
* Dump the given variables and end the script
*/
function dd(...$vars) {
dump(...$vars);
die();
}
}
if (!function_exists('prettyPrint')) {
/**
* Pretty print a variable with proper formatting
*/
function prettyPrint($var, $indent = 0, $visited = []) {
$indentStr = str_repeat(' ', $indent);
$type = gettype($var);
// Handle circular references
if (is_object($var) || is_array($var)) {
$hash = is_object($var) ? spl_object_hash($var) : md5(serialize($var));
if (in_array($hash, $visited)) {
return is_object($var) ? '*CIRCULAR REFERENCE*' : '*CIRCULAR ARRAY REFERENCE*';
}
$visited[] = $hash;
}
switch ($type) {
case 'NULL':
return 'null';
case 'boolean':
return $var ? 'true' : 'false';
case 'integer':
case 'double':
return (string) $var;
case 'string':
$length = strlen($var);
$preview = strlen($var) > 100 ? substr($var, 0, 100) . '...' : $var;
return '"' . $preview . '"' . ($length > 0 ? " ({$length} chars)" : '');
case 'array':
if (empty($var)) {
return '[]';
}
$output = "[\n";
$isAssoc = array_keys($var) !== range(0, count($var) - 1);
foreach ($var as $key => $value) {
$output .= $indentStr . ' ';
if ($isAssoc) {
$output .= (is_string($key) ? '"' . $key . '"' : $key) . ' => ';
}
$output .= prettyPrint($value, $indent + 1, $visited) . ",\n";
}
$output .= $indentStr . ']';
return $output;
case 'object':
$className = get_class($var);
$output = "{$className} {\n";
// Handle stdClass and dynamic objects differently
if ($className === 'stdClass') {
// For stdClass, just iterate through public properties
foreach (get_object_vars($var) as $name => $value) {
$output .= $indentStr . " public \${$name}: ";
$output .= prettyPrint($value, $indent + 1, $visited) . "\n";
}
} else {
// For other objects, use reflection to get all properties
$reflection = new ReflectionClass($var);
$properties = [];
// Public properties
foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) {
$properties[$prop->getName()] = [
'visibility' => 'public',
'value' => $prop->getValue($var)
];
}
// Protected properties
foreach ($reflection->getProperties(ReflectionProperty::IS_PROTECTED) as $prop) {
$prop->setAccessible(true);
$properties[$prop->getName()] = [
'visibility' => 'protected',
'value' => $prop->getValue($var)
];
}
// Private properties
foreach ($reflection->getProperties(ReflectionProperty::IS_PRIVATE) as $prop) {
$prop->setAccessible(true);
$properties[$prop->getName()] = [
'visibility' => 'private',
'value' => $prop->getValue($var)
];
}
// Also get any dynamic properties that might not be declared
foreach (get_object_vars($var) as $name => $value) {
if (!isset($properties[$name])) {
$properties[$name] = [
'visibility' => 'dynamic',
'value' => $value
];
}
}
foreach ($properties as $name => $info) {
$output .= $indentStr . " {$info['visibility']} \${$name}: ";
$output .= prettyPrint($info['value'], $indent + 1, $visited) . "\n";
}
}
$output .= $indentStr . '}';
return $output;
case 'resource':
return 'resource(' . get_resource_type($var) . ')';
default:
return $type . '(' . var_export($var, true) . ')';
}
}
}
// Optional: Add some helper functions for better debugging experience
if (!function_exists('ddd')) {
/**
* Dump and die with a more detailed trace
*/
function ddd(...$vars) {
echo '<div style="background: #fff3cd; border: 1px solid #ffeaa7; border-radius: 4px; padding: 15px; margin: 10px 0; font-family: Consolas, monospace; font-size: 13px;">';
echo '<h4 style="margin: 0 0 10px 0; color: #856404;">Debug Trace</h4>';
$trace = debug_backtrace();
foreach (array_slice($trace, 0, 5) as $i => $step) {
$file = isset($step['file']) ? basename($step['file']) : 'unknown';
$line = $step['line'] ?? 'unknown';
$function = $step['function'] ?? 'unknown';
echo "<div style='color: #856404; font-size: 11px;'>{$i}: {$file}:{$line} in {$function}()</div>";
}
echo '</div>';
dd(...$vars);
}
}
if (!function_exists('ray')) {
/**
* Simple ray() alternative for quick debugging
*/
function ray(...$vars) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
$file = basename($trace['file'] ?? 'unknown');
$line = $trace['line'] ?? 'unknown';
error_log("🔍 Ray Debug [{$file}:{$line}]: " . print_r($vars, true));
return count($vars) === 1 ? $vars[0] : $vars;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment