php 如何编写与 Laravel 相同的 DD() 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41669246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write own DD() function same as laravel?
提问by Shankar Thiyagaraajan
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,
我使用 laravel 很长时间了,但目前我使用 wordpress。我喜欢使用 laravel 的DD()
功能。但是在wordpress中我只能使用这些,
print_r(),
var_dump(),
var_export()....
These all are just expand entire arrayor object. But I need laravel's expandand closemechanism for handling array and object. I use the following as general ddneed,
这些都只是扩展整个数组或对象。但是我需要 laravel 的扩展和关闭机制来处理数组和对象。我使用以下作为一般dd需要,
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 ?
是否可以 ?
回答by Red Panda
Laravel's dd
uses symfony's VarDump component. Then you'll have 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.
Laraveldd
使用 symfony 的VarDump 组件。然后,您将拥有一个全局可用的dump
函数来格式化输出。唯一的区别是它不会在转储后“死亡”,您必须手动执行此操作 - 但在大多数情况下,这甚至不是您想要的。
- Run
composer global require symfony/var-dumper
(assuming you have composer in your wordpress project) - Add
auto_prepend_file = ${HOME}/.composer/vendor/autoload.php
to your php.ini file; - From time to time, run
composer global update symfony/var-dumper
to have the latest bug fixes.
- 运行
composer global require symfony/var-dumper
(假设你的 wordpress 项目中有 composer) - 添加
auto_prepend_file = ${HOME}/.composer/vendor/autoload.php
到您的 php.ini 文件中; - 不时运行
composer global update symfony/var-dumper
以获取最新的错误修复。
Here is the documentation for the VarDumper component. https://symfony.com/doc/current/components/var_dumper.html
这是 VarDumper 组件的文档。https://symfony.com/doc/current/components/var_dumper.html
So your dd
function could look like this:
所以你的dd
函数可能是这样的:
if (!function_exists('dd')) {
function dd()
{
array_map(function($x) {
dump($x);
}, func_get_args());
die;
}
}
回答by Ngoc Nam
I updated more functions and latest code of d
functions below in debug functionspackage.
我d
在调试函数包中更新了更多函数和最新的函数代码。
(Below answer is about 1 year ago.)
(下面的答案是大约一年前的。)
======================================
======================================
Below is my own code. It can work in plain PHP (no framework).
下面是我自己的代码。它可以在普通的 PHP 中工作(无框架)。
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";
}
回答by Rajitha Bandara
For others who might be interested in Laravel like dd(), please check this repo -
对于可能对 dd() 等 Laravel 感兴趣的其他人,请查看此 repo -
回答by Bitclaw
You can bring in this namespace in your class use Illuminate\Support\Debug\Dumper;
and then use it for your variables like this:
你可以在你的类中引入这个命名空间use Illuminate\Support\Debug\Dumper;
,然后将它用于你的变量,如下所示:
(new Dumper)->dump($myVariable);
回答by Arun Code
The Laravel dd
is actually a great function. However, the undergoing mechanism is var_dump
and then die
.
Laraveldd
实际上是一个很棒的函数。然而,发生机制是var_dump
和 然后die
。
Example:
例子:
$arr = [1,2,3,4,5,6];
var_dump($arr);
die();
回答by Lucas Bustamante
I do it like this:
我这样做:
function dd($a){
var_dump($a);
exit;
}
I use it all the time. Even created a Snippet for it in Sublime.
我用它所有的时间。甚至在 Sublime 中为它创建了一个片段。
I also use var_masterpieceChrome extension to get a nice output, where I can expand and collapse the array keys, etc.
我还使用var_masterpieceChrome 扩展程序来获得不错的输出,我可以在其中展开和折叠数组键等。
回答by Husnain Aslam
I do like this:
我喜欢这样:
function dd($var){
echo "<pre>";
print_r($var);
exit;
}
回答by Abdo-Host
composer require --dev symfony/var-dumper
作曲家需要 --dev symfony/var-dumper
<?php
if (! function_exists('dd')) {
/**
* Dump the passed variables and end the script.
*
* @param mixed
* @return void
*/
function dd()
{
array_map(function ($value) {
if (class_exists(Symfony\Component\VarDumper\Dumper\CliDumper::class)) {
$dumper = 'cli' === PHP_SAPI ?
new Symfony\Component\VarDumper\Dumper\CliDumper :
new Symfony\Component\VarDumper\Dumper\HtmlDumper;
$dumper->dump((new Symfony\Component\VarDumper\Cloner\VarCloner)->cloneVar($value));
} else {
var_dump($value);
}
}, func_get_args());
die(1);
}
}
?>
回答by Mehran
it works like a charm.
它就像一个魅力。
function dd()
{
array_map(function($x) { var_dump($x); }, func_get_args()); die;
}
回答by chebaby
Most of the time i work with laravel framework, when it came to debugging the dd()
helper method become a very handful tool.
大多数时候我使用 laravel 框架,当涉及到调试dd()
辅助方法时,它成为了一个非常少的工具。
But recently i was requested to work on a symfony 3.4 project, at first i use dump($my_vars); die();
to dump and die my vars. But that became quickly very combersome. So i ended up by adding a dd()global helper method to the symfony 3.4 project, here is how:
但是最近我被要求在一个 symfony 3.4 项目上工作,起初dump($my_vars); die();
我习惯于转储并杀死我的变量。但这很快变得非常麻烦。所以我最终在 symfony 3.4 项目中添加了一个dd()全局辅助方法,方法如下:
Inside src/
folder i created Support/
folder, inside of the Support/
folder i created helpers.php
file where i added my dump helper functions.
里面src/
的文件夹我创建Support/
的文件夹,将里面Support/
的文件夹,我创建helpers.php
文件,其中我将自己倾倒的辅助功能。
src/Support/helpers.php
源代码/支持/helpers.php
if (!function_exists('dd')) {
/**
* Dump the passed variables and end the script.
*
* @return void
*/
function dd() {
$args = func_get_args();
call_user_func_array('dump', $args);
die(1);
}
}
You may need to add the new file (helpers.php
) to your composer.json
file to be loaded for you automatically, like this:
您可能需要将新文件 ( helpers.php
)添加composer.json
到要自动加载的文件中,如下所示:
composer.json
作曲家.json
{
...
"autoload": {
"psr-4": {
"App\": "src/"
},
"files": [
"src/Support/helpers.php"
]
},
...
}