21

I'm wondering whether in the situation where I'm extending a class that has already 'use' keyword above it to use specific namespace - do I need to add another 'use' above the inheriting class to use the same namespace? Situation like this:

namespace Core;

use System\Plugin;

class Front extends Application { }

and now in the Controller, which is called directly without the namespace (using full path):

use System\Plugin;

class PageController extends Front { }

or would it work without 'use' as well and allow me to use the System\Plugin namespace:

class PageController extends Front { }

?

Spencer Mark
  • 5,263
  • 9
  • 29
  • 58

1 Answers1

26

No, you need the "use" statement in both files. Use is a file-level keyword and isn't affected by inheritance.

See the scoping rules for importing and the little box describing what I said at the bottom of the manual page.

Lusitanian
  • 11,012
  • 1
  • 41
  • 38
  • Obviously if within the Front class I've instantiated an object from within the Plugin namespace then I can use it without the 'use System\Plugin' in the inherited class. – Spencer Mark Aug 03 '12 at 11:41
  • Of course, and you don't always need a use statement either. Using the fully-qualified namespace works as well. – Lusitanian Aug 03 '12 at 11:45
  • 9
    It'd be nice (i.e. DRYer) if classes could inherit `use` statements from parent classes – hohner Jun 26 '13 at 18:52