开发者

Can I rename a namespace in PHP?

开发者 https://www.devze.com 2023-04-03 10:35 出处:网络
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 :

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

精彩评论

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