89

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

6
  • 2
    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. Commented Aug 19, 2011 at 12:40
  • 2
    Ofcourse, but I don't know how to put it in other words. Commented Aug 19, 2011 at 12:44
  • I thought so, but to avoid confusion :) Commented Aug 19, 2011 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/* Commented Mar 18, 2016 at 21:09
  • 2
    @Ultimater Nice addition. If you're okay with enabling eval support this might be a solution. Personally, I wouldn't recommend it. Commented Nov 23, 2016 at 14:29

2 Answers 2

88

This is not possible in PHP.

All you can do is:

namespace Foo;

use Bar;

$obj = new Bar\SomeClassFromBar();
Sign up to request clarification or add additional context in comments.

7 Comments

Or of course, you can put concrete classes in the use clausule. (use Bar\SomeClassFromBar; $obj = new SomeClassFromBar();)
Coming from C# world, this didn't sound very pleasing. :- (
@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#
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 actually use at the top level like this is always global, and at the bottom of this example, they recommend not prepending the backslash.
|
4

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.