当前位置:首页 > PHP教程 > php函数 > 列表

PHP全局使用Laravel辅助函数dd

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-01 20:29:10 浏览: 评论:0 

在本篇文章里小编给大家整理了关于PHP全局使用Laravel辅助函数dd的相关知识点,需要的朋友们学习下。

dump() 方法

全局 composer.json

  1. "require": { 
  2.   "squizlabs/php_codesniffer": "*", 
  3.   "fxp/composer-asset-plugin": "^1.4", 
  4.   "symfony/var-dumper": "3.3.16" 
  5. } 

配置PHP.ini

auto_prepend_file = "C:UsersMSAppDataRoamingComposervendorautoload.php"

更新Composer

composer global update

更新后重启apache就可以全局使用函数 dump()

dd() 方法

全局 composer.json

  1. # 新增 autoload 
  2. "autoload": { 
  3.   "files": [ 
  4.     "D:/web/php/debugHelper.php" 
  5.   ] 
  6. } 

新建 debugHelper.php

  1. # install symfony/var-dump to your project 
  2. # composer require symfony/var-dumper 
  3.  
  4. // use namespace 
  5. use ComponentVarDumperClonerVarCloner; 
  6. use ComponentVarDumperDumperCliDumper; 
  7. use ComponentVarDumperDumperHtmlDumper as SymfonyHtmlDumper; 
  8.  
  9. /** 
  10.  * Class HtmlDumper 
  11.  */ 
  12. class HtmlDumper extends SymfonyHtmlDumper 
  13. { 
  14.   /** 
  15. 大专栏 PHP 全局使用 Laravel 辅助函数 ddnt">   * Colour definitions for output. 
  16.    * 
  17.    * @var array 
  18.    */ 
  19.   protected $styles = [ 
  20.     'default' => 'background-color:#fff; color:#222; line-height:1.2em; font-weight:normal; font:12px Monaco, Consolas, monospace; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:100000', 
  21.     'num' => 'color:#a71d5d', 
  22.     'const' => 'color:#795da3', 
  23.     'str' => 'color:#df5000', 
  24.     'cchr' => 'color:#222', 
  25.     'note' => 'color:#a71d5d', 
  26.     'ref' => 'color:#a0a0a0', 
  27.     'public' => 'color:#795da3', 
  28.     'protected' => 'color:#795da3', 
  29.     'private' => 'color:#795da3', 
  30.     'meta' => 'color:#b729d9', 
  31.     'key' => 'color:#df5000', 
  32.     'index' => 'color:#a71d5d', 
  33.   ]; 
  34. } 
  35.  
  36. /** 
  37.  * Class Dumper 
  38.  */ 
  39. class Dumper 
  40. { 
  41.   /** 
  42.    * Dump a value with elegance. 
  43.    * 
  44.    * @param mixed $value 
  45.    * @return void 
  46.    */ 
  47.   public function dump($value) 
  48.   { 
  49.     if (class_exists(CliDumper::class)) { 
  50.       $dumper = 'cli' === PHP_SAPI ? new CliDumper : new HtmlDumper; 
  51.       $dumper->dump((new VarCloner)->cloneVar($value)); 
  52.     } else { 
  53.       var_dump($value); 
  54.     } 
  55.   } 
  56. } 
  57.  
  58. if (! function_exists('dd')) { 
  59.   /** 
  60.    * Dump the passed variables and end the script. 
  61.    * 
  62.    * @param mixed 
  63.    * @return void 
  64.    */ 
  65.   function dd(...$args) 
  66.   { 
  67.     foreach ($args as $x) { 
  68.       (new Dumper)->dump($x); 
  69.     } 
  70.     die(1); 
  71.   } 
  72. } 
  73.  
  74. if (! function_exists('dda')) { 
  75.   /** 
  76.    * Dump the passed array variables and end the script. 
  77.    * 
  78.    * @param mixed 
  79.    * @return void 
  80.    */ 
  81.   function dda(...$args) 
  82.   { 
  83.     foreach ($args as $x) { 
  84.       (new Dumper)->dump($x->toArray()); 
  85.     } 
  86.     die(1); 
  87.   } 
  88. } 

更新composer自动加载

composer global dump-autoload

更新后就可以全局使用函数 dd()

Tags: PHP全局 Laravel辅助函数

分享到: