6

I upgraded from phpstorm7 to phpstorm8. Now, whenever I create a new PHP class in an existing folder, the namespace is inserted correctly. Yet when I create an empty folder and create in there a new class, the namespace is empty and I would have to manually add it.

In pictures:

  1. Existing folder has the namespace:
    Example of the namesapce being inserted for an existing folder
  2. New folder in the same project lacks the namespace. Example of the namespace not being generated for a new folder

How can I tell phpstorm to always autogenerate the current namespace?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • It's hard to say what is wrong .. but most likely you do not have marked certain folders as Namespace roots. Please show screenshot of `Preferences | Directories` (since you are on Mac). **P.S.** You may also try `Code | Detect PSR-0 Namespace Roots` if you have not got them set up yet. – LazyOne Sep 19 '14 at 15:26
  • 1
    Will it help? -> http://blog.jetbrains.com/phpstorm/2014/04/psr-0-psr-4-and-sourcetest-root-support-in-phpstorm-8-eap/ – Vipin Kr. Singh Apr 26 '15 at 10:26
  • 1
    This is an old post, but since I ran into it, I figured other people might too. I managed to fix this issue by going into `Settings > Project: xxx > Directories` and clicking the little 'P' next to my `src` directory and setting a root namespace for my project. – Robin Kanters Jan 21 '16 at 08:29
  • @RobinKanters Thanks for pointing that out. You may add that as an answer for me to upvote, even though it wasn't my original issue. I just ran into something similar where no matter what I tried the default namespace was always wrong in one project, and the solution provided in your comment helped. – k0pernikus Feb 15 '16 at 16:00

1 Answers1

10

Sometimes PhpStorm doesn't correctly detect the appropriate namespace for your source directories.

You could try to fix this by going into Settings > Project: xxx > Directories, clicking the little 'P' next to your src/ directory and setting a root namespace for your project.

enter image description here (you may not have as much directories listed in the right panel, depending on the complexity of your project)

Robin Kanters
  • 5,018
  • 2
  • 20
  • 36
  • 2
    To do this automatically you can use setting called 'Synchronize IDE Settings with composer.json'. It supports PSR-0 and PSR-4. Be aware that enabling this option affects some other settings (however they're useful) too. – panK Jan 31 '19 at 11:48