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();
}
?>
精彩评论