GetUsed is a powerful tool that automatically generates PHP use
statements for your code. It scans your PHP files to detect all classes, functions, and constants being used, then creates properly formatted import statements ready to paste at the top of your file.
Instead of using fully qualified names with backslashes (\Exception
, \json_encode()
), proper imports make your code cleaner and more readable.
Missing backslashes in namespaced code can cause hard-to-debug errors. Using proper imports eliminates this risk entirely.
Manually tracking and writing imports is tedious. GetUsed generates them all in seconds with a single command.
With the included VSCode integration, you can generate imports with a keyboard shortcut (Ctrl+Shift+T).
If a class moves to a different namespace, you only need to update the import statement, not every occurrence in your code.
GetUsed analyzes your PHP code to:
- Detect classes, interfaces, and traits being used
- Find function calls that could benefit from imports
- Identify constants referenced in your code
- Generate properly formatted
use
statements - Comment out imports that already exist in your file
VSCode Screenshot
The use
statement in PHP allows you to import classes, functions, and constants from other namespaces. For example, if you call json_encode()
within a namespaced class, PHP first searches in the current namespace before falling back to the global namespace.
You can use backslashes like \json_encode()
to reference the global namespace directly, but this makes code less readable. A better approach is to import what you need at the top of your file:
// Import a class
use DateTime;
// Import a function
use function json_encode;
// Import a constant
use const PHP_EOL;
// Import with an alias
use function MyNamespace\myJsonEncoder as json_encode;
GetUsed automates this process by analyzing your code and generating all necessary import statements.
# create directory if not exist
mkdir -p ~/bin/many
# enter directory
cd ~/bin/many
# clone GetUsed
git clone https://github.com/eypsilon/get-used.git
# make it executable
chmod -v 770 ~/bin/many/get-used/GetUsed.php
~/bin/many/get-used/GetUsed.php /path/to/src/AnyClass.php
cd ~/bin/many/get-used/www/used
php -S localhost:8000
Then open localhost:8000 in your browser.
# Edit your bash aliases
~$ sudo gedit ~/.bash_aliases
# Add this line
alias GetUsed='~/bin/many/get-used/GetUsed.php'
# Refresh aliases
~$ source ~/.bash_aliases
Now you can simply use:
GetUsed /path/to/src/AnyClass.php
# Get help
GetUsed -h
Add this to your ~/.config/Code/User/keybindings.json
:
{
"key": "ctrl+shift+t",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "GetUsed ${file}\u000D" }
}
Now you can press Ctrl+Shift+T on any open PHP file to generate import statements instantly.
// file = /path/to/src/Http/Curler.php
// start = 1661266197.6779
// end = 1661266197.7762
/** defined(0), taken(0), constant(2), class(2), function(2), total(6) */
use DateTime;
use DateTimeZone;
use function array_keys;
use function array_merge;
use const PHP_EOL;
use const JSON_UNESCAPED_SLASHES;
If imports already exist in your file, GetUsed will comment them out to avoid duplicates.
Web Interface
Terminal Output
- Original tool by Engin Ypsilon
- README editted by Claude, Anthropic's AI assistant