Debug PHP? Aww, hell no!
Not when some kind of bizarro script will do it for you! Check out PHPStan! (Zbornak)
What is PHPStan? PHPStan (as in PHP Standards) checks PHP source code for errors based on the coding standards set forth by the PHP Framework Interoperability Group. Easily install this static analysis tool for PHP code using the PHP Composer package manager.
If PHPStan finds errors in your code, you should use the feedback it provides to make your code comply with the standards. PHPStan provides a report of undefined variables and constants, or functions which aren't passed the correct number of arguments. The detection level is adjustable, so you can start out allowing anything-goes, and gradually increase strictness. It's definitely worth trying!
- Install Composer to Try PHPStan
-
Install PHPStan using PHP Composer. Composer is a package manager for PHP that allows you to easily manage and install dependencies for your project.
You must provide a valid path to a PHP executable. If you're using WSL or Linux, type
whereis php. On Windows/XAMPP, something like:C:\Xampp\php\php.exe
# TO USE PHPStan with WORDPRESS: cd /var/www/html composer require --dev phpstan/phpstan composer require phpstan/extension-installer composer require --dev szepeviktor/phpstan-wordpress composer show phpstan/extension-installer
VS Code users may want the PHPStan Visual Studio Code extension.
For WordPress developers, there is a PHPStan WordPress extension with rules specifically for WordPress.
Using PHPStan
Sometimes we want to see what is happening in the code or have notes in the output to keep everything in mind. Place this inside a function you are debugging or anywhere for basic info about execution.
Manual Debugging Suggestion:
// __FILE__ - full path and filename echo 'File: ' . __FILE__ . ':' . __LINE__ . '
';// FUNCTION - function name echo 'At function: ' . FUNCTION . ':' . LINE . '
';// PATHINFO_BASENAME echo 'this->basename - ' . pathinfo(FILE, PATHINFO_BASENAME) . ':' . LINE . '
';
Version II:
PHP Classes and Methods:
Using get_defined_vars() in methods of a WordPress plugin shows values scoped to that method. In global scope, output can be too verbose.
$option = get_option('plugin_options');
$filteredoptions = apply_filters('plugin_loaded', get_option('plugin_options'));
echo 'file: ' . FILE . ' : line ' . LINE . '
';
echo CLASS . '::' . METHOD . '
';
var_dump(get_defined_vars());
- Expand Your PHP
-
- What is a PHP Magic Constant?
- Which WordPress Core functions rely on pathinfo()?