5

I want the PhpStorm to insert current namespace by default or with a hotkey or with live templates.

Whet I create new Php class ("New | PHP Class" dialog) there is a field for a namespace. Is there a way for it to be filled automatically? It does not look a big deal because in my case a namespace is just directory path (I use composer) starting from src. My search efforts did not give my anything about this at all. Looks like PhpStrom does not have this feauture. But maybe there is some plugin or a hack?

2
  • "Whet I create new Php class there is a field for a namespace. Is there a way for it to be filled automatically?" Where? In "New | PHP Class" dialog? it is filled automatically. If it does not -- you did not configure your project correctly (e.g. no Source Root defined). Commented Mar 4, 2018 at 17:42
  • @LazyOne you are right. Did not configure source root Commented Mar 4, 2018 at 17:56

1 Answer 1

12

New | PHP Class dialog should fill the namespace automatically based on the directory where file will be created. If it does not then you have not configured your project properly.

Settings/Preferences | Directories -- ensure that your src folder is marked as Source Root. That's depends on project, of course: for Laravel project you need to map app folder to App\ namespace (typical case).

PhpStorm can also detect source roots from your composer.json settings and can even keep it in sync since 2017.2 version (see https://blog.jetbrains.com/phpstorm/2017/07/configuring-with-composer-in-phpstorm-2017-2/).

Some links to read (official help pages):

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to insert namespace in other ways? I mean not in New | PHP Class. Is there some snippet or a hotkey?
Please share it. I need it in case I moved a file to a different directory for example.
@FreeLightman "Please share it" Oh, sorry -- I've missed "not" word there. "I need it in case I moved a file to a different directory for example" use appropriate refactoring (e.g. invoke "Refactor | Move" on class name) instead of just moving files -- it takes care of that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.