Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a PHP namespace shortcut for this?

Tags:

namespaces

php

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 :)

like image 775
JREAM Avatar asked May 15 '12 02:05

JREAM


2 Answers

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;
like image 74
deceze Avatar answered Oct 12 '22 16:10

deceze


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.

like image 22
Matthew Avatar answered Oct 12 '22 15:10

Matthew