PHP Classes

How to Obtain the PHP Namespaces Used in a Class Using the Get Used Class: Parse PHP script to extract the namespaces it uses

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-04-20 (10 hours ago) RSS 2.0 feedNot enough user ratingsTotal: 36 All time: 11,013 This week: 41Up
Version License PHP version Categories
get-used 1.0.3MIT/X Consortium ...7.4Tools, Console, Code Generation, PHP 7
Description 

Author

This class can parse a PHP script to extract the namespaces it uses.

It can take a given PHP class source script and parse it to determine which namespaces the class uses in class, functions, and constants are used to add any missing "use" statements.

The package provides scripts to run the class from the command line or the PHP Web server.

Picture of Engin Ypsilon
  Performance   Level  
Name: Engin Ypsilon <contact>
Classes: 7 packages by
Country: Germany Germany
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Details

GetUsed - PHP Namespace Import Generator

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.

Why Use This Tool?

1. Clean, Readable Code

Instead of using fully qualified names with backslashes (\Exception, \json_encode()), proper imports make your code cleaner and more readable.

2. Prevent Errors

Missing backslashes in namespaced code can cause hard-to-debug errors. Using proper imports eliminates this risk entirely.

3. Save Time

Manually tracking and writing imports is tedious. GetUsed generates them all in seconds with a single command.

4. IDE Integration

With the included VSCode integration, you can generate imports with a keyboard shortcut (Ctrl+Shift+T).

5. Maintainability

If a class moves to a different namespace, you only need to update the import statement, not every occurrence in your code.

How It Works

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__

Visual Studio Code Example Response

Technical Background

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.

Installation

# 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

Usage

From Terminal

~/bin/many/get-used/GetUsed.php /path/to/src/AnyClass.php

Via Web Interface

cd ~/bin/many/get-used/www/used
php -S localhost:8000

Then open localhost:8000 in your browser.

Set an Alias (recommended)

# 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

VSCode Integration

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.

Example Output

// 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.

Screenshots

__Web Interface__

Web Interface Example Response

__Terminal Output__

Terminal Example Response

Authors


  Files folder image Files (11)  
File Role Description
Files folder imagesrc (1 file)
Files folder imagewww (1 directory)
Accessible without login Plain text file GetUsed.config.php Aux. Auxiliary script
Accessible without login Plain text file GetUsed.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (11)  /  src  
File Role Description
  Plain text file Used.php Class Class source

  Files folder image Files (11)  /  www  
File Role Description
Files folder imageused (1 file, 1 directory)

  Files folder image Files (11)  /  www  /  used  
File Role Description
Files folder imageassets (5 files)
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files (11)  /  www  /  used  /  assets  
File Role Description
  Accessible without login Image file favicon.ico Data Auxiliary data
  Accessible without login Image file screenshot-sw-terminal-out.png Data Auxiliary data
  Accessible without login Image file screenshot-sw.png Data Auxiliary data
  Accessible without login Image file screenshot-vscode.png Data Auxiliary data
  Accessible without login Plain text file style.css Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:36
This week:0
All time:11,013
This week:41Up