PHP全局使用Laravel辅助函数dd

在本篇文章里小编给大家整理了关于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. // use namespace
  4. use ComponentVarDumperClonerVarCloner;
  5. use ComponentVarDumperDumperCliDumper;
  6. use ComponentVarDumperDumperHtmlDumper as SymfonyHtmlDumper;
  7. /**
  8. * Class HtmlDumper
  9. */
  10. class HtmlDumper extends SymfonyHtmlDumper
  11. {
  12. /**
  13. 大专栏 PHP 全局使用 Laravel 辅助函数 ddnt"> * Colour definitions for output.
  14. *
  15. * @var array
  16. */
  17. protected $styles = [
  18. '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',
  19. 'num' => 'color:#a71d5d',
  20. 'const' => 'color:#795da3',
  21. 'str' => 'color:#df5000',
  22. 'cchr' => 'color:#222',
  23. 'note' => 'color:#a71d5d',
  24. 'ref' => 'color:#a0a0a0',
  25. 'public' => 'color:#795da3',
  26. 'protected' => 'color:#795da3',
  27. 'private' => 'color:#795da3',
  28. 'meta' => 'color:#b729d9',
  29. 'key' => 'color:#df5000',
  30. 'index' => 'color:#a71d5d',
  31. ];
  32. }
  33. /**
  34. * Class Dumper
  35. */
  36. class Dumper
  37. {
  38. /**
  39. * Dump a value with elegance.
  40. *
  41. * @param mixed $value
  42. * @return void
  43. */
  44. public function dump($value)
  45. {
  46. if (class_exists(CliDumper::class)) {
  47. $dumper = 'cli' === PHP_SAPI ? new CliDumper : new HtmlDumper;
  48. $dumper->dump((new VarCloner)->cloneVar($value));
  49. } else {
  50. var_dump($value);
  51. }
  52. }
  53. }
  54. if (! function_exists('dd')) {
  55. /**
  56. * Dump the passed variables and end the script.
  57. *
  58. * @param mixed
  59. * @return void
  60. */
  61. function dd(...$args)
  62. {
  63. foreach ($args as $x) {
  64. (new Dumper)->dump($x);
  65. }
  66. die(1);
  67. }
  68. }
  69. if (! function_exists('dda')) {
  70. /**
  71. * Dump the passed array variables and end the script.
  72. *
  73. * @param mixed
  74. * @return void
  75. */
  76. function dda(...$args)
  77. {
  78. foreach ($args as $x) {
  79. (new Dumper)->dump($x->toArray());
  80. }
  81. die(1);
  82. }
  83. }

更新composer自动加载

composer global dump-autoload

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