Although most of the convenience methods will be removed from CakePHP going forward (see the note about basics.php), and I say good riddance, you still have the opportunity to add your own to bootstrap.php.
For example a couple that I like are wrappers for var_dump()
. The main reason being is that var_dump()
will properly display NULL
and FALSE
. Unlike debug()
and pr() (alias to) print_r()
, which will show… well… nothing.
So here you go:
function vd($var) { if(Configure::read() > 0) { echo '<pre>'; echo var_dump($var); echo '</pre>'; } }
Or, if you’d like to die()
out:
function vdd($var) { if(Configure::read() > 0) { die(var_dump($var)); } }
function vd($var) {
if (Configure::read() > 0) {
foreach($var as $a => $b) {
echo $a;
if(is_array($b)) {
vd($b);
} else {
echo $b;
}
}
}
}
Typed too fast. Replace echo $b with var_dump($b) :)
I think there is a small mistake in the vdd function. The close tag for pre will never occur because is after die().
In this way it works fine.
function vdd($var) {
if(Configure::read() > 0) {
echo '
';
die(); // or exit;
}
}
Anyway is not a relevant mistake. You make a great job.
@CAT Shannon
LOL, Duh… very good point :) Anyways in that case the
pre
tags are not really needed anyway. I’ve just simplified the function a little. But your suggestions is just as good.@petteyg359
Thanks for sharing.
what about prd ?
function prd($var){
if(Configure::read() > 0) {
pr($var);
die();
}
}
The wrapper functions may save you a few keystrokes, or many if you use them often, but they slow down execution. Using a wrapper function inside of your own function saves you a few keystrokes one time only. Just type the full function name…
@petteyg359
Exactly, that’s why they are being removed from the usage inside the core. However, when developing they are simply short-cuts and in case of someone accidentally forgetting to remove print_r() in production, can actually quite valuable.
@Angel S. Moreno
Same problem, doesn’t properly display certain values.
A side note, it is actually quite nice to implement a solution like: http://krumo.sourceforge.net/
(Hmm… I should probably do an example for cake…)