jphase
jphase

Reputation: 396

Can a child class inherit namespace aliases from a parent class in PHP?

Is it possible to have a parent class that uses namespaces setup namespace aliases for a child class that extends it? This is what I'm trying to accomplish... Here's my parent class:

namespace ez\app;
use ez\app\do as do;

class DefaultController {
   public static function operation(){
      // Do operation
   }
}

And then have the following child class inherit the namespace alias of ez\app\do at the top:

namespace ez\app;

class controller extends \ez\app\DefaultController {
   do::operation();
}

Upvotes: 4

Views: 2027

Answers (1)

sectus
sectus

Reputation: 15464

No, you must define alias for every class.

The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations.

...

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

http://php.net/manual/en/language.namespaces.importing.php

Upvotes: 4

Related Questions