86

I'm implementing namespaces in my existing project. I found that you can use the keyword 'use' to import classes into your namespace. My question is, can I also import all the classes from 1 namespace into another. Example:

namespace foo
{

    class bar
    {

        public static $a = 'foobar';

    }

}

namespace
{
    use \foo;  //This doesn't work!
    echo bar::$a;
}

Update for PHP 7+

A new feature in PHP 7 is grouped declarations. This doesn't make it as easy as using 1 'use statement' for all the classes in a given namespace, but makes it somewhat easier...

Example code:

<?php
// Pre PHP 7 code
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;

// PHP 7+ code
use some\namespace\{ClassA, ClassB, ClassC as C};
?>

See also: https://secure.php.net/manual/en/migration70.new-features.php#migration70.new-features.group-use-declarations

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
Rob
  • 2,466
  • 3
  • 22
  • 40
  • 1
    Mind you that "Import" does not mean that you can actually find that class. You still need to have that class available trough either a manual require or an autoloader. – Nanne Aug 19 '11 at 12:40
  • 1
    Ofcourse, but I don't know how to put it in other words. – Rob Aug 19 '11 at 12:44
  • I thought so, but to avoid confusion :) – Nanne Aug 19 '11 at 13:28
  • When I read the update on PHP7 I frowned. I'm bummed PHP didn't realize how convenient it would be to open up ALL of the classes in a namespace. In java you just have `use foo/*` – eggmatters Mar 18 '16 at 21:09
  • It's ugly, but the `import_namespace('Hamcrest', __NAMESPACE__);` implementation defined over here will get the job done: http://stackoverflow.com/questions/3358099/calling-a-php-function-defined-in-another-namespace-without-the-prefix – Ultimater Sep 30 '16 at 09:14
  • 1
    @Ultimater Nice addition. If you're okay with enabling eval support this might be a solution. Personally, I wouldn't recommend it. – Rob Nov 23 '16 at 14:29

2 Answers2

85

This is not possible in PHP.

All you can do is:

namespace Foo;

use Bar;

$obj = new Bar\SomeClassFromBar();
Ondřej Mirtes
  • 5,054
  • 25
  • 36
  • 5
    Or of course, you can put concrete classes in the use clausule. (`use Bar\SomeClassFromBar; $obj = new SomeClassFromBar();`) – Ondřej Mirtes Aug 19 '11 at 13:44
  • 5
    Coming from C# world, this didn't sound very pleasing. :- ( – dotNET Oct 05 '18 at 07:20
  • 2
    @dotNET I love PHP until then I was introduced to real OOP. Im still using PHP but this is one of the reasons why I might convert to C# – Alex Coroza Oct 14 '18 at 11:40
  • 1
    aside: you should really prepend backslash to the namespace. Otherwise, you're saying it's relative to the Foo namespace. Many times this assumed neglect bites people in the butt. Just get into the habit of explicitly specifying the root namespace if it is the root namespace and not relative to the current namespace (even if the current namespace may happen to be root) – ahnbizcad Nov 06 '20 at 22:37
  • @ahnbizcad actually `use` at the top level like this is always global, and at the bottom of [this example](https://www.php.net/manual/en/language.namespaces.importing.php#example-287), they recommend not prepending the backslash. – Jacob Phillips Mar 17 '21 at 22:21
  • @JacobPhillips yes, IF use is used at the top, then yes. otherwise, no. But agreed that it's better to declare use at the top. I'd even say it's better to do `use Bar\SomeClassFromBar` then do `new SomeClassFromBar()` – ahnbizcad Mar 19 '21 at 19:03
  • Also, if leading backslash is not recommended, then the language should disallow it or not have allowed it in the first place. allowing flexibility of syntax for the same thing isn't a benefit, it's a negative benefit, as it causes unnecessary deviance, reduces searchability, allows for the bad way to occur and become exemplary, adds confusion, is another thing to learn and know about just to ultimately not use it. – ahnbizcad Mar 19 '21 at 19:08
5

You can use the "as" for shortening and aliasing long namespaces

composer.json

{
    "autoload": {
        "psr-4": {
            "Lorem\\Ipsum\\": "lorem/ipsum",
            "Lorem\\Ipsum\\Dolor\\": "lorem/ipsum/dolor",
            "Lorem\\Ipsum\\Dolor\\Sit\\": "lorem/ipsum/dolor/sit"
        }
    }
}  

index.php

    <?php

    use Lorem\Ipsum\Dolor\Sit as FOO;

    define ('BASE_PATH',dirname(__FILE__));
    require BASE_PATH.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php';

    $bar = new FOO\Bar();
    $baz = new FOO\Baz();
    $qux = new FOO\Qux();
observer
  • 2,925
  • 1
  • 19
  • 38
András Szabácsik
  • 1,957
  • 1
  • 16
  • 11