We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

print_r alternative for life

I am beginner. A useful feature that shows where the print_r is coming from. what do you think? please upgrade if you know better

<?php
// without exit
function pr() {
    $trace = debug_backtrace();

    $msgTemplate = 'Debug called from %s (line %s) ';
    echo '<h3>', sprintf($msgTemplate, $trace[0]['file'], $trace[0]['line']), '<h3>';

    $separator = ' ++++++++ DEBUG +++++++ ';
    echo '<h2>' . __FUNCTION__ . $separator . '</h2>';
    $args = func_num_args();
    for ($i = 0; $i < $args; $i++) {
        echo '<pre>';
        echo print_r(func_get_arg($i), true);
        echo '</pre>';
    }
    echo '<h2>' . __FUNCTION__ . $separator . '</h2>';

}
// with exit 
function prx() {
    $trace = debug_backtrace();

    $msgTemplate = 'Debug called from %s (line %s) ';
    echo '<h3>', sprintf($msgTemplate, $trace[0]['file'], $trace[0]['line']), '<h3>';

    $separator = ' ++++++++ DEBUG +++++++ ';
    echo '<h2>' . __FUNCTION__ . $separator . '</h2>';
    $args = func_num_args();
    for ($i = 0; $i < $args; $i++) {
        echo '<pre>';
        echo print_r(func_get_arg($i), true);
        echo '</pre>';
    }
    echo '<h2>' . __FUNCTION__ . $separator . '</h2>';
    exit;
}

A few notes about that:

  • prx() should just call pr(), then exit. That'll save a lot of code duplication
  • You can loop through a function's arguments with func_get_args() directly - no need to use func_num_args() and func_get_arg()
  • You might as well just call print_r(), instead of passing true as the second argument, then echoing what print_r returns. That's needlesly complicated.

Here's what I use:

function dump($passed,$use_vardump = FALSE,$backtrace_offset = 0)
{
    $is_cli = (php_sapi_name() == 'cli');
    $newline = ($is_cli) ? "\n" : '<br />';
    $backtrace = debug_backtrace();
    $file = $backtrace[$backtrace_offset]['file'];
    $line = $backtrace[$backtrace_offset]['line'];

    if(!$is_cli){
        echo '<pre>';
    }
    echo "File: $file$newline";
    echo "Line: $line$newline";

    if($use_vardump){
        var_dump($passed);
    }
    else{
        if(is_array($passed)){
            print_r($passed);
        }
        else if(is_object($passed)){
            if(method_exists($passed, 'toArray')){
                print_r($passed->toArray());
            }
            else{
                print_r($passed);
            }
        }
        else{
            echo $passed;
        }
    }
    if(!$is_cli){
        echo '</pre>';
    }

    if(count(ob_get_status()) != 0){
        ob_flush();
    }
}

function dieDump($passed,$use_vardump=FALSE){
    dump($passed,$use_vardump,1);
    exit();
}

Of particular note is the fact that if an object is encountered, it is checked for the existence of toArray(), which it then uses. That prevents Phalcon models from kicking out all their scaffolding info and just gives me the relevant info in the object.