Nov 3, 2019

Make PHP lib functions like dump() or dd() globally available

It happens that I work with PHP outside of Laravel or Symfony, typically when working on a lib, and I always miss the dump and dd function. Do you remember var_dump or print_r? I don't want to have to use that!

I found a simple way to always preload PHP library. I shouldn't say preload tho, this as nothing to do with the PHP 7.4 preload feature. It's basically a way to include a php file, before the normal PHP cycle starts. This is especially useful if you're working on a simple 15-line script without Composer.

It's all built-in PHP and it has been here for a looong time: https://php.net/auto-prepend-file.

How to always load symfony/var-dumper

  1. Create a new directory somewhere (typically in your dotfiles/

  2. Run composer require symfony/var-dumper

  3. Create a simple prepend.php with the content shown below
    Note: I don't recommend defining the isYolo() function.

__torchlight-block-[b2145057-9149-4323-817b-e91ac59bc286]__
  1. Open your php.ini configuration file and add the following
__torchlight-block-[dad0091e-dae7-44a6-8a82-c1ea77a4c952]__
  1. You can now access dump, dd, isYolo from anywhere, including psysh console!

I have been using this for over a year now, I never encountered any confict, error like "function already defined" or whatsoever.