Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write own DD() function same as laravel?

Tags:

I used laravel for a long time but currently I work with wordpress. I loved using the laravel's DD() function. But in wordpress I can only use these,

   print_r(),    var_dump(),    var_export().... 

These all are just expand entire array or object. But I need laravel's expand and close mechanism for handling array and object. I use the following as general dd need,

if (!function_exists('dd')) {  function dd()   {       echo '<pre>';       array_map(function($x) {var_dump($x);}, func_get_args());       die;    }  } 

It works well, but I need a styled and organised form of listing.

Is it possible ?

like image 298
Shankar Thiyagaraajan Avatar asked Jan 16 '17 04:01

Shankar Thiyagaraajan


People also ask

How will you explain DD () function in laravel?

dd stands for "Dump and Die." Laravel's dd() function can be defined as a helper function, which is used to dump a variable's contents to the browser and prevent the further script execution.

How does with work in laravel?

This with() method is used from a certain model which is specified inside this method. For example, if you have model called 'Country' and method called 'city', you would write Country::with('city') inside your controller or wherever you want. Shortly, it enables you to use method of a model.

What is collection in laravel?

Laravel collection is a useful feature of the Laravel framework. A collection works like a PHP array, but it is more convenient. The collection class is located in the Illuminate\Support\Collection location. A collection allows you to create a chain of methods to map or reduce arrays.


2 Answers

Laravel's dd uses symfony's VarDump component. It provides a globally available dump function which formats the output. The only difference is that it won`t "die" after the dump, you'll have to do that manually - but in most cases that isn't even something you'd want.

  1. Run composer global require symfony/var-dumper (assuming you have composer in your wordpress project)
  2. Add auto_prepend_file = ${HOME}/.composer/vendor/autoload.php to your php.ini file;
  3. From time to time, run composer global update symfony/var-dumper to have the latest bug fixes.

Here is the documentation for the VarDumper component. https://symfony.com/doc/current/components/var_dumper.html

Before var-dumpr version 4.1:

Declare a dd function, dumping all arguments and stopping the process:

if (!function_exists('dd')) {     function dd()     {         foreach (func_get_args() as $x) {             dump($x);         }         die;     }  } 

After var-dump version 4.1:

Since var-dumper version 4.1, dd() is already declared. Loading the library is enough.

like image 169
Coloured Panda Avatar answered Nov 15 '22 14:11

Coloured Panda


I updated more functions and latest code of d functions below in debug functions package.

(Below answer is about 1 year ago.)

======================================

Below is my own code. It can work in plain PHP (no framework).

function d($data){     if(is_null($data)){         $str = "<i>NULL</i>";     }elseif($data == ""){         $str = "<i>Empty</i>";     }elseif(is_array($data)){         if(count($data) == 0){             $str = "<i>Empty array.</i>";         }else{             $str = "<table style=\"border-bottom:0px solid #000;\" cellpadding=\"0\" cellspacing=\"0\">";             foreach ($data as $key => $value) {                 $str .= "<tr><td style=\"background-color:#008B8B; color:#FFF;border:1px solid #000;\">" . $key . "</td><td style=\"border:1px solid #000;\">" . d($value) . "</td></tr>";             }             $str .= "</table>";         }     }elseif(is_resource($data)){         while($arr = mysql_fetch_array($data)){             $data_array[] = $arr;         }         $str = d($data_array);     }elseif(is_object($data)){         $str = d(get_object_vars($data));     }elseif(is_bool($data)){         $str = "<i>" . ($data ? "True" : "False") . "</i>";     }else{         $str = $data;         $str = preg_replace("/\n/", "<br>\n", $str);     }     return $str; }  function dnl($data){     echo d($data) . "<br>\n"; }  function dd($data){     echo dnl($data);     exit; }  function ddt($message = ""){     echo "[" . date("Y/m/d H:i:s") . "]" . $message . "<br>\n"; } 
like image 43
Ngoc Nam Avatar answered Nov 15 '22 15:11

Ngoc Nam