PHPで変数の内容をダンプする関数といえばvar_dump()ですね。
During development in PHP, you may use var_dump() function as below when you want to dump one or more variables.
$n = 10; $s = "hello"; $b = true; $a = array("key" => "value"); var_dump($n, $s, $b, $a);
出力結果は以下のようになります。
The above code will output:
int(10)
string(5) "hello"
bool(true)
array(1) {
["key"]=>
string(5) "value"
}
Zend Frameworkにも変数の内容をダンプするZend_Debug::dump()が標準で用意されています。ただし、Zend_Debug::dump()のシンタックスは、
class Zend_Debug { public static function dump($var, $label=null, $echo=true); }
となっているので、var_dump()は出力結果を無条件で標準出力に送ってしまう一方、Zend_Debug::dump()は第3引数の$echoをfalseにすれば、ダンプ内容を文字列として受け取れるのがメリットではありますが、var_dump()のように複数の変数を一度に渡せないのが使い勝手の悪いところ。しかも、Zend_Debug::dump()はvar_dump()をラップしただけのものなので、出力結果はvar_dump()と基本的に大差なくあまり重宝しません。
Zend Framework provides Zend_Debug::dump() to dump a variable. If you give false to third argument $echo, you can retrieve the result as a string. On the other hand, var_dump() will send the result to the stdout directly, which means the result will be displayed immediately, it may be problem sometime. However, Zend_Debug::dump() is not useful if you want to dump multiple variables at once, because it accepts only one variable as you can see above. And the output is not different from var_dump() so much, I’m not sure the reason why I should use Zend_Debug::dump() instead of var_dump().
一方、PEARには同じように変数をダンプするVar_Dumpパッケージがあります。PEARを使って開発していた頃は非常に重宝したパッケージです。特に多層配列やオブジェクトなどの複雑な構造をした変数をダンプする際に役立ちます。しかし、Zend_Debug::dump()と同じように、1回の関数呼び出しで単一の変数しかダンプできない問題を抱えています。
class Var_Dump { function display($expression, $return=false, $options=null, $rendererOptions=null); }
PEAR provides Var_Dump package which has similar function with var_dump() or Zend_Debug::dump(), and I have often used this package during development with PEAR. Especially, it is very useful to dump variables with complex structure such as nested-array or object. But Var_Dump::display() function has the same problem as Zend_Debug::dump() that it accepts single variable to dump.
そこで、Var_Dumpのレンダリング機能を利用しつつ、一度に複数の変数を渡せるようなZend FrameworkのビューヘルパーH2plus_View_Helper_DumpVarを実装してみました。ダウンロードはこちらから。
Then, I implemented H2plus_View_Helper_DumpVar which works as a View Helper in Zend Framework by using renderer classes in PEAR::Var_Dump, and provides function which accepts multiple variables at once. You can download here.
インストール – Install
前述したように、このビューヘルパーはPEAR::Var_Dumpに依存しているので、利用している環境にあらかじめVar_Dumpパッケージをインストールしておく必要があります。共有サーバーにPEARをインストールする方法も参考にどうぞ。
This helper depends on PEAR::Var_Dump as described above, so PEAR::Var_Dump package must be installed on your system.
以下のようなディレクトリ構造を使っていると仮定して、application/views/helpersディレクトリにDumpVar.phpを解凍します。
Assuming directory structure as shown below, just place extracted DumpVar.php in application/views/helpers directory.
application/
controllers/
views/
filters/
helpers/
scripts/
Zend Frameworkリファレンスガイドの「53.4.2. ヘルパーのパス」に従って、コントローラスクリプト内でヘルパーパスを設定する必要があります。
In your controller script, you should specify search path for view helper as described in Zend Framework Reference Guide “59.4.2. Helper Paths”.
<?php // IndexController.php function indexAction() { $this->view->addHelperPath( '/home/h2plus/zf/application/views/helpers', 'H2plus_View_Helper' ); } ?>
使用方法 – Usage
使い方はいたって簡単。ビュースクリプト内でダンプしたい変数を$this->dumpVar()に渡すだけです。ただし、dumpVarヘルパーはダンプ内容を文字列として返すので、その場で表示させる場合はechoしてください。
It is very simple. To dump variables in your view script, just pass them to $this->dumpVar(). Note that dumpVar helper returns output as string, so you should call echo if you want to display immediately.
<?php // index.phtml $n = 10; $s = "hello"; $b = true; $a = array("key" => "value"); echo $this->dumpVar($n, $s, $b, $a); ?>
出力結果は以下のようになります。
The above code will output:
int 10
string(5) hello
bool true
array(1) {
key => string(5) value
}
また、スタティック関数setOptions()を使うと、PEAR::Var_Dumpのレンダラーの切り替えや各変数の区切りなどをカスタマイズすることができます。
By using static function setOptions(), you can switch renderer class provided by PEAR::Var_Dump, or specify custom separator between each variable.
H2plus_View_Helper_DumpVar::setOptions(array $options=array());
$optionsには、以下のキーを持つ連想配列を指定します。
$options should be an associative array which has the following keys:
renderer
PEAR::Var_Dumpが提供するレンダラー。デフォルトは'HTML4_Text'。
Renderer provided by PEAR::Var_Dump.'HTML4_Text'by default.separator
各変数の区切り文字列。
Separator string between each variable’s dump.prefix
コンテンツの先頭に付加する文字列。
String prepend to dump content.postfix
コンテンツの末尾に付加する文字列。
String append to dump content.
最近のコメント