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));
}
}
9 Responses to “Couple of convenience methods to add to your bootstrap.php”
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) :)
on July 3rd, 2010 at 11:12 AM #
[...] This post was mentioned on Twitter by Dennis James. Dennis James said: Reading: Couple of convenience methods to add to your bootstrap.php: Although most of the convenience methods will… http://bit.ly/ciQTm4 [...]
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 '
'; var_dump($var); 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
pretags 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 great point! thanks for the tip! Did some benchmarks and the performance is night and day, best way to go is definitely non-wrapper functions inside my wrapper functions.
@teknoid if you are too busy with a cakephp-krumo then I could jump in.
@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…)