Hi!

> In C++ when you type:
> using std;
> 
> Then you can use all methods/classes/whatever is defined in std
> without typing std.
> so: std::cout becomes just cout.

PHP namespaces do not work like that. In PHP, namespace is a permanent
part of the class/function name. All namespace translations are done in
compile-time (which is not the same as compile-time in C++, btw) and are
simple alias translations.

PHP explicitly avoids mass imports, because this would lead to context
pollution and interoperability problems - i.e., if you have some
function named "foo" in two namespaces and you import both, you may have
problems. And since imported namespaces usually mean somebody else's
code, you may not have control or good information about changes, making
importing code fragile. Since unlike C++ PHP does not have compiler that
can resolve all the mess before it is run, PHP has to be more
conservative in order to avoid problems.
For this reason, PHP's "use" is nothing more than aliasing operator. It
does not put any other names into global space but the names
specifically mentioned by it. So, if you have to do std::cout, it'd
still be std\cout, but if you want to do
some\long\import\namespace\cout, you can alias
some\long\import\namespace to mystd, and do mystd\cout. So you never
have to do more than one \, but you still have to do one, to avoid
polluting global space.

> But i'm wondering why the "use Some\Long\Namespace" doesn't work like
> the C++ one. Since i would have guessed that adding that use will give
> me access to all the methods/classes/whatever that live within that
> namespace _without_ having to prefix it with the last part of the
> namespace.

This is exactly what we were trying to avoid - for the reasons described
above.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to