Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically import the namespace of a class in VS Code?

I want to add the namespace of a php class automatically with a shortcut/extension in vscode, but I can't find how to do this.

<?php 

namespace App\Models;


class Article {
}
like image 972
MelWet Avatar asked Oct 05 '18 10:10

MelWet


3 Answers

Use this handy PHP Namespace Resolver extension for Visual Studio Code.

How to use:

  1. Right click on any class name and select Import Class to automatically import it's namespace at the top of the file
  2. Press CTRL+ALT+i on Windows / CTRL+Option+i on MacOS

https://i.imgur.com/upEGtPa.gif

like image 165
Arslan Ramay Avatar answered Nov 18 '22 14:11

Arslan Ramay


Another way is to

  1. highlight the class by double click on the class name.

  2. press ctrl + space keys and then from the list, the namespace of the specific class can be seen.

  3. select the desired namespace from the list by up-down arrow keys

  4. press the Tab key to import the namespace automatically at above the page.

Hope this helps,

like image 38
zahra_oveyedzade Avatar answered Nov 18 '22 12:11

zahra_oveyedzade


PHP Intelephense is the best extension I've ever used for this.

It provides both autocomplete and auto-import.

like image 3
Ahmed Ismail Avatar answered Nov 18 '22 14:11

Ahmed Ismail