Last active
December 17, 2015 16:39
-
-
Save yuka2py/5640724 to your computer and use it in GitHub Desktop.
CakePHP の debug 関数が心地よかったので、簡易版を書いたので貼っておきます。
あくまで簡易版 (*'-'*)。ちなみに WordPress 案件中に作ったので、WordPress 記法になっているのがミソですw $var は、出力したい変数。
$label は、ついでに出力したい文字列。
$echo は、true の場合のみ出力する。条件付きで出力したいとき、if 書く手間をほんの少し減らすことができます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Output the contents of the variable along with the file and line number. | |
* @param mixed $var output variable of any types. | |
* @param string $label optional. default is null. | |
* @param boolean $echo optional. default is true. | |
* @return void | |
*/ | |
function debug( $var, $label=null, $echo=true ) { | |
if ( ! $echo ) { | |
return; | |
} | |
ob_start(); | |
echo "<br>\n"; | |
echo "<strong>"; | |
$trace = debug_backtrace(); | |
if ( isset( $trace[1] ) ) { | |
echo $trace[0]['file']; | |
echo ": " . $trace[0]['line'] . " at "; | |
if ( empty( $trace[1]['type'] ) ) { | |
echo $trace[1]['function']; | |
} else { | |
echo $trace[1]['class'] . $trace[1]['type'] . $trace[1]['function']; | |
} | |
} | |
if ( $label ) { | |
echo " ($label)"; | |
} | |
echo "</strong>\n"; | |
echo "<pre>"; | |
var_dump( $var ); | |
echo "</pre>"; | |
$output = ob_get_clean(); | |
$output = preg_replace( "/^([ ]+)\[/m", '$1', $output ); | |
$output = preg_replace( "/]=>\n\s+/", ' => ', $output ); | |
$output = preg_replace( "/array\(0\) \{\n\s*\}\n/", "array(0) { }\n", $output ); | |
echo $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
出力はざっと↓みたいな感じになります。