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 View files (11)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-08-25 (3 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 29 All time: 10,645 This week: 189Up
Version License PHP version Categories
get-used 1.0.2MIT/X Consortium ...7.4Utilities and Tools, Console, Code Ge..., P...
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
Name: Engin Ypsilon <contact>
Classes: 4 packages by
Country: Germany Germany
Age: 42
All time rank: 4236225 in Germany Germany
Week rank: 587 Up32 in Germany Germany Up
Innovation award
Innovation award
Nominee: 1x

Details

Namespace helper - create use Namespace; Statements

This package can parse PHP scripts to extract the names of Classes, Functions and Constants used within the script. The parsed names will then be used to generate a list of use Namespace; statements for the parsed script. It can parse any script, but only namespaced Classes allows the use of the generated statements.

__VSCode Screenshot__

It's pretty hard to talk about this Topic, because it's just called "Namespace" ...

Visual Studio Code Example Response

The use statement can be used in Classes to tell PHP, which Function to use internally for Functions used in the Class. It also can boost scripts by pointing PHP to the right Namespace to use. For example, if you call json_encode() within a Class, PHP searches in the calling Class for a Function with the name json_encode(), before searching it in the global Namespace (if at all). You can speed up the process with a Backslash before the function name, like: \json_encode(), but it looks awful. An alternate is to define any used Function, Class and Constant at the very top of the Class with the 'use' statement. This Package is made to simplify the process.

The use statement also allows quick Aliasing of Functions in namespaced Classes.

namespace Any;
use function myOwnJsonEcode as json_encode;
# if you now call "json_encode()" in the class,
# PHP will use "myOwnJsonEcode()" to execute it.

Install Many\Dev\Used

# create directory if not exist
mkdir -p ~/bin/many

# enter directory
cd ~/bin/many

# clone Many\Dev\Used
git clone https://github.com/eypsilon/get-used.git

# make it executable (user+group = rwx)
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 using PHPs dev-server__

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

and open localhost:8000

Set an Alias (optional)

Feel free to set one you feel comfortable with.

~$ sudo gedit ~/.bash_aliases
# put
alias GetUsed='~/bin/many/get-used/GetUsed.php'
# refresh aliases
~$ source ~/.bash_aliases
GetUsed /path/to/src/AnyClass.php
# Get help -h | info -i | config -c
GetUsed -h

Used Keywords in Visual Studio Code

You can use this Package also in VSCode. Set a key combination in ~/.config/Code/User/keybindings.json

{
    "key": "ctrl+shift+t",
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "GetUsed ${file}\u000D" }
}

and hit the combo on open Files to get use Namespace; statements on the fly.

Example output

If the generated use Namespace; statements are already defined in the script, the generated ones will get commented out.

GetUsed /path/to/src/Http/Curler.php

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

Screenshots

__Web Interface for GetUsed__

Web Interface Example Response

__Terminal__ The screenshot is taken from the web interface, but it looks identical in Terminal.

Terminal Example Response

  Files folder image Files  
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  /  src  
File Role Description
  Plain text file Used.php Class Class source

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

  Files folder image Files  /  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  /  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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:29
This week:0
All time:10,645
This week:189Up