Skip to content

Instantly share code, notes, and snippets.

@yuka2py
Last active December 17, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuka2py/5640724 to your computer and use it in GitHub Desktop.
Save yuka2py/5640724 to your computer and use it in GitHub Desktop.
CakePHP の debug 関数が心地よかったので、簡易版を書いたので貼っておきます。 あくまで簡易版 (*'-'*)。ちなみに WordPress 案件中に作ったので、WordPress 記法になっているのがミソですw $var は、出力したい変数。 $label は、ついでに出力したい文字列。 $echo は、true の場合のみ出力する。条件付きで出力したいとき、if 書く手間をほんの少し減らすことができます。
<?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;
}
@yuka2py
Copy link
Author

yuka2py commented May 24, 2013

出力はざっと↓みたいな感じになります。

/home/foreignkey/www/wp-content/plugins/wp-devutil/functions.php: 16 at {closure}
object(WP_Query)#122 (44) {
  "query_vars" => array(48) {
    "page" => string(0) ""
    "pagename" => string(5) "about"
    "error" => string(0) ""
    "m" => int(0)
    "p" => int(0)
    "post_parent" => string(0) ""
    "subpost" => string(0) ""
    "subpost_id" => string(0) ""
    "attachment" => string(0) ""
    "attachment_id" => int(0)
    "name" => string(0) ""
    "static" => string(0) ""
    "page_id" => int(0)
    "second" => string(0) ""
    "minute" => string(0) ""
    "hour" => string(0) ""
    "day" => int(0)
    "monthnum" => int(0)
    "year" => int(0)
    "w" => int(0)
    "category_name" => string(0) ""
    "tag" => string(0) ""
    "cat" => string(0) ""
    "tag_id" => string(0) ""
    "author_name" => string(0) ""
    "feed" => string(0) ""
    "tb" => string(0) ""
    "paged" => int(0)
    "comments_popup" => string(0) ""
    "meta_key" => string(0) ""
    "meta_value" => string(0) ""
    "preview" => string(0) ""
    "s" => string(0) ""
    "sentence" => string(0) ""
    "fields" => string(0) ""
    "menu_order" => string(0) ""
    "category__in" => array(0) { }
    "category__not_in" => array(0) { }
    "category__and" => array(0) { }
    "post__in" => array(0) { }
    "post__not_in" => array(0) { }
    "tag__in" => array(0) { }
    "tag__not_in" => array(0) { }
    "tag__and" => array(0) { }
    "tag_slug__in" => array(0) { }
    "tag_slug__and" => array(0) { }
    "post_parent__in" => array(0) { }
    "post_parent__not_in" => array(0) { }
  }
  "tax_query" => NULL
  "meta_query" => bool(false)
  "post_count" => int(0)
  "current_post" => int(-1)
  "in_the_loop" => bool(false)
  "comment_count" => int(0)
  "current_comment" => int(-1)
  "found_posts" => int(0)
  "max_num_pages" => int(0)
  "max_num_comment_pages" => int(0)
  "is_single" => bool(false)
  "is_preview" => bool(false)
  "is_page" => bool(true)
  "is_archive" => bool(false)
  "is_date" => bool(false)
  "is_year" => bool(false)
  "is_month" => bool(false)
  "is_day" => bool(false)
  "is_time" => bool(false)
  "is_author" => bool(false)
  "is_category" => bool(false)
  "is_tag" => bool(false)
  "is_tax" => bool(false)
  "is_search" => bool(false)
  "is_feed" => bool(false)
  "is_comment_feed" => bool(false)
  "is_trackback" => bool(false)
  "is_home" => bool(false)
  "is_404" => bool(false)
  "is_comments_popup" => bool(false)
  "is_paged" => bool(false)
  "is_admin" => bool(false)
  "is_attachment" => bool(false)
  "is_singular" => bool(true)
  "is_robots" => bool(false)
  "is_posts_page" => bool(false)
  "is_post_type_archive" => bool(false)
  "query_vars_hash" => string(32) "24ce6baed22bdb10fcc79db47544ec7e"
  "query_vars_changed" => bool(false)
  "thumbnails_cached" => bool(false)
  "query" => array(2) {
    "page" => string(0) ""
    "pagename" => string(5) "about"
  }
  "queried_object" => object(WP_Post)#2055 (24) {
    "ID" => int(24)
    "post_author" => string(1) "1"
    "post_date" => string(19) "2013-05-01 18:36:54"
    "post_date_gmt" => string(19) "2013-05-01 09:36:54"
    "post_content" => string(0) ""
    "post_title" => string(12) "会社概要"
    "post_excerpt" => string(0) ""
    "post_status" => string(7) "publish"
    "comment_status" => string(4) "open"
    "ping_status" => string(4) "open"
    "post_password" => string(0) ""
    "post_name" => string(5) "about"
    "to_ping" => string(0) ""
    "pinged" => string(0) ""
    "post_modified" => string(19) "2013-05-24 09:10:54"
    "post_modified_gmt" => string(19) "2013-05-24 00:10:54"
    "post_content_filtered" => string(0) ""
    "post_parent" => int(0)
    "guid" => string(37) "http://wp36.foreignkey.jp/?page_id=24"
    "menu_order" => int(0)
    "post_type" => string(4) "page"
    "post_mime_type" => string(0) ""
    "comment_count" => string(1) "0"
    "filter" => string(3) "raw"
  }
  "queried_object_id" => int(24)
}

/home/foreignkey/www/wp-content/plugins/wp-devutil/functions.php: 17 at {closure}
object(WP_Post)#2055 (24) {
  "ID" => int(24)
  "post_author" => string(1) "1"
  "post_date" => string(19) "2013-05-01 18:36:54"
  "post_date_gmt" => string(19) "2013-05-01 09:36:54"
  "post_content" => string(0) ""
  "post_title" => string(12) "会社概要"
  "post_excerpt" => string(0) ""
  "post_status" => string(7) "publish"
  "comment_status" => string(4) "open"
  "ping_status" => string(4) "open"
  "post_password" => string(0) ""
  "post_name" => string(5) "about"
  "to_ping" => string(0) ""
  "pinged" => string(0) ""
  "post_modified" => string(19) "2013-05-24 09:10:54"
  "post_modified_gmt" => string(19) "2013-05-24 00:10:54"
  "post_content_filtered" => string(0) ""
  "post_parent" => int(0)
  "guid" => string(37) "http://wp36.foreignkey.jp/?page_id=24"
  "menu_order" => int(0)
  "post_type" => string(4) "page"
  "post_mime_type" => string(0) ""
  "comment_count" => string(1) "0"
  "filter" => string(3) "raw"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment