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.
symfony/var-dumper
How to always load -
Create a new directory somewhere (typically in your dotfiles/
-
Run
composer require symfony/var-dumper
-
Create a simple
prepend.php
with the content shown below
Note: I don't recommend defining theisYolo()
function.
1<?php
2
3require_once __DIR__.'/vendor/autoload.php';
4
5// Define here any global function, constant or class you want
6
7function isYolo()
8{
9 return rand() % 2;
10}
- Open your
php.ini
configuration file and add the following
1; Automatically add files before PHP document.
2; http://php.net/auto-prepend-file
3auto_prepend_file = /absolute/path/to/your/prepend.php
- 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.