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

Never use var_dump() again, even when you work outside Laravel or Symfony

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.

<?php

require_once __DIR__.'/vendor/autoload.php';

// Define here any global function, constant or class you want

function isYolo()
{
  return rand() % 2;
}

  1. Open your php.ini configuration file and add the following
; Automatically add files before PHP document.
; http://php.net/auto-prepend-file
auto_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.