7

I'm trying to find a way to mass apply these namespaces, as this would be inconvenient to write out. I know I can simply do, use jream\ as j but I would like to see if it's possible to avoid the backslash.

require '../jream/Autoload.php';

use jream\Autoload as Autoload,
    jream\Database as Database,
    jream\Exception as Exception,
    jream\Form as Form,
    jream\Hash as Hash,
    jream\Output as Output,
    jream\Registry as Registry,
    jream\Session as Session;

new Autoload('../jream');

Isn't there a way to say something along these lines: jream\\* as *; ?

Any tips would be appreciated :)

JREAM
  • 5,741
  • 11
  • 46
  • 84

2 Answers2

6

At the very least you can skip all the redundant as aliasing:

use jream\Autoload,
    jream\Database,
    jream\Exception,
    jream\Form,
    jream\Hash,
    jream\Output,
    jream\Registry,
    jream\Session;

If you want to use everything in the namespace without typing it out one by one, then indeed the only real choice is to alias the namespace as a whole and use a backslash:

use jream as j;

new j\Autoload;
deceze
  • 510,633
  • 85
  • 743
  • 889
3

Isn't there a way to say something along these lines: jream\* as *; ?

No, but you can do this:

// use-jream.php
class Autoload extends jream\Autoload {}
class Database extends jream\Database {}
...

// index.php
require_once 'use-jream.php'

new Autoload('../jream');

But I wouldn't really recommend doing it.

And of course, if you want to just change the default namespace:

namespace jream;

new Autoload('../jream');

That is all an import jream.* could ever mean in PHP, since PHP has absolutely no way to determine if a class could possibly exist in a certain namespace unless you inform it.

Matthew
  • 47,584
  • 11
  • 86
  • 98