20
23

More than 5 years have passed since last update.

PHPにラムダ式が入ったので、出力のキャプチャが簡単になった

Last updated at Posted at 2014-04-25

PHPにラムダ式が入ったのは結構前ですが、これでvar_dumpなんかが少し楽になるので紹介。

function capture($func) {
  ob_start();
  $func();
  $ret = ob_get_contents();
  ob_end_clean();
  return $ret;
}

// var_dump の戻り値が取れる
$debug_info = capture(function() { var_dump($my_obj); });

// 実行したスクリプトの標準出力結果が取れる
$command_out = capture(function() { system("ls -al"); });

あってよかったラムダ式。
あとはもう少し構文が短ければよかったのに

20
23
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
23