The dd() function in each PHP project

Sanasar Yuzbashyan
3 min readApr 25, 2022

Hi guys. Today we will look on how we can add a very powerful dd() function to any PHP project in just a few steps. I think you will agree with me that this is much more convenient than var_dump() or print_r().
I wanted to share this information with you because when I needed to get this result I couldn’t find a working solution. Everything I found failed in the last steps. And now I’ll show you how I set everything up.
So let’s get started.
We will use The VarDumper Component, but you can also use spatie/global-ray. At the very beginning I used spatie/global-ray and now I will describe the problem that I had. The installation and use is very simple and I expected everything to work, but here’s what I had as a result.

composer global require spatie/global-rayglobal-ray install

Here I demonstrated for php-cli. But you have to find and use the correct php.ini file that your project uses. For example, you can call phpinfo() and see the result.
Now let’s go back to the error and try to understand what happened.
Note. I am using Ubuntu 20.04 and all the examples here are shown for my OS.

The main error is permission denied. And this is correct, since we run the global-ray install command on behalf of an OS user who is not a root user and accordingly cannot change the php.ini file.
And when we try to run as root sudo global-ray install, then we have this error
sudo: global-ray: command not found.
Paradox. The last thing left for me to try was to manually change the php.ini file as root and set this value.
auto_prepend_file = /home/developer/.config/composer/vendor/spatie/global-ray/src/scripts/global-ray-loader.php
But that didn’t help either because I got the permission denied error again.

Well, let’s solve this paradox.

composer global require symfony/var-dumper:^4.4

In my case, version 4.4 worked.

Create a directory on your home partition. I called it php_auto_prepend_script

Copy the entire composer folder from .config/ to php_auto_prepend_script/.
Set the permission so that php can have access to this directory. In my case, php works as an apache module and my apache server name is www-data.

sudo chown -R $USER:www-data php_auto_prepend_script/

sudo chmod -R g+rwx php_auto_prepend_script/

You may check what name your PHP is operating under by running this code in a php file exec(‘whoami’).

And at last, edit your php.ini file and set the following value.

auto_prepend_file = /home/developer/php_auto_prepend_script/composer/vendor/autoload.php

You should set this depending on the username of your OS.

sudo service apache2 restart

That’s all. Good luck to you.

--

--