Stanislav Malyshev wrote:
> Hi!
>
>> instantiates a class in the foo\bar namespace named buh. In other
>> words, it instantiates class buh from the sub-namespace bar of namespace
>> foo.
>
> Why there's "subnamespace bar of namespace foo" and not just "namespace
> foo\bar"? When such notation is useful?
The reason is that "foo\bar" works very differently in practice from a
simple "foo_bar" name. You must use the whole name "foo_bar" but can
use part of the "foo\bar" name with aliasing or within the foo namespace
as in:
<?php
namespace foo_bar {
function func(){}
}
namespace foo {
bar\func();
}
?>
This does not work (obviously)
<?php
namespace foo_bar {
function func(){}
}
namespace foo {
bar();
}
?>
Perhaps we can put that question to the readers of php-doc: do you see a
utility in defining nested namespaces as I have done in the sample docs?
>> The important line is "MyNamespace2\j" which resolves the same way as in
>> the C++ example.
>
> C++ has nesting, we do not - it's the difference.
I'm curious what fits your definition of nesting? Is there some
difference in name resolution that PHP does not do that would make it
have "nested namespaces"?
The only difference between the examples below is declarative syntax of
nested namespaces. C requires nested namespaces to be bracketed and
doesn't allow :: in namespace name, PHP requires full declaration of
namespace name with \ separator, but everything else is exactly the
same. Examples:
C++:
nsname::nested::func();
PHP:
nsname\nested\func();
C++:
import nsname::nested;
nested::func();
PHP:
use nsname\nested;
nested\func();
C++:
namespace nsname {
namespace nested {
func();
}
}
PHP:
namespace nsname\nested;
func();
C++:
namespace nsname {
namespace nested {
void func(){}
}
}
PHP:
namespace nsname\nested;
function func(){}
Thanks,
Greg