user142019
user142019

Reputation:

Can I rename a namespace in PHP?

In C++ one can do this:

namespace qux = std::foo::bar::baz;
qux::CFoo BAR;

Can one do such a thing in PHP?

Upvotes: 3

Views: 2394

Answers (2)

salathe
salathe

Reputation: 51950

Namespaces may be aliased (docs).

The general idea is use … as …; as shown below.

use std\foo\bar\baz as qux;
qux\CFoo();

And here's a try-this-at-home example:

<?php
namespace std\foo\bar\baz {
    function CFoo() {
        echo 'hello, world';
    }
}

namespace {
    use std\foo\bar\baz as qux;
    qux\CFoo();
}
?>

Upvotes: 2

Alfwed
Alfwed

Reputation: 3282

You can do this :

namespace foo\bar\baz;
use foo\bar\baz as renamed;

new renamed\cFoo(); // Points to foo\bar\baz\cFoo()

See the documentation for further details.

Upvotes: 7

Related Questions