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:

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.

3require_once __DIR__.'/vendor/autoload.php';
5// Define here any global function, constant or class you want
7function isYolo()
9 return rand() % 2;
  1. Open your php.ini configuration file and add the following
1; Automatically add files before PHP document.
3auto_prepend_file = /absolute/path/to/your/prepend.php
  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.