How do I dump and print variables in Yii for debugging? I would like to use var_dump()
or print_r()
. I tried to use Yii::trace()
but it crashes with this error in runtime/logs/app.log
. It doesn't even tell me the line in my code that it fails.
2015-03-18 20:54:11 [::1][-][-][warning][yii\log\Dispatcher::dispatch] Unable to send log via yii\debug\LogTarget: Exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'
in /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2-debug/LogTarget.php:58
Stack trace:
#0 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2-debug/LogTarget.php(58): serialize(Array)
#1 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2-debug/LogTarget.php(112): yii\debug\LogTarget->export(Array)
#2 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2/log/Dispatcher.php(183): yii\debug\LogTarget->collect(Array, true)
#3 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2/log/Logger.php(170): yii\log\Dispatcher->dispatch(Array, true)
#4 [internal function]: yii\log\Logger->flush(true)
#5 {main}
Reference http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
Because you are asking about something like var_dump
and print_r
, I can advise built-in helper for this. It's called yii\helpers\VarDumper. Yii::trace() is for logging trace messages.
VarDumper is intended to replace the buggy PHP function
var_dump
andprint_r
.It can correctly identify the recursively referenced objects in a complex object structure. It also has a recursive depth control to avoid indefinite recursive display of some peculiar variables.
VarDumper can be used as follows,
VarDumper::dump($var);
Personally I don't use it, just tried only couple of times for testing.
I think is better to use Xdebug for that purposes.
See also PsySH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With