开发者

Can I rename a namespace in PHP?

In C++ one can do this:

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

Can one d开发者_StackOverflowo such a thing in PHP?


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.


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();
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜