There’s a lot to be said for using a traditional debugger such as Xdebug (and I’m still a fan), but lately, I’ve been writing more generalized PHP utilities that have no front-end and aren’t plugged into any specific application.
That is, I’m using native PHP libraries to run queries against incoming data and help analyze certain aspects of it and output a report.
There’s been a learning curve for a lot of it, but part of the process has included plenty of debugging. And if you’ve read my series on Ray in WordPress, then you know I’m a fan of Ray.
So here’s how you can use Ray to debug vanilla PHP utilities.
In your composer.json
file, make sure the following directive is present:
"require-dev": { "spatie/ray": "^1.29.0"
},
Then in the terminal, run the following:
$ composer update
You may need to run $ composer install
if you haven’t already, but I’m assuming you’re adding to the file you already have set up.
After that’s done, make sure the following is in your PHP code:
<?php require 'vendor/autoload.php'; // ... use Spatie/Ray;
At this point, add the following line just to make sure Ray is loading (and make sure the Ray application is actually running). Add the following line:
ray('Loaded...');
Then in your terminal run $ php script.php
. Assuming all has gone well, you should see the following:
![](https://blog.fastdot.com/wp-content/uploads/2023/11/use-ray-to-debug-vanilla-php-utlities-tom-mcfarlin.png)
From there, you can use the rest of the tools outlined in the Ray for WordPress series for doing whatever it is you want. Except at this point, you’re just running Ray for debug vanilla PHP utilities.
New context, same functions. Easy enough!