Sorry for the ambiguous subject line, but I could not find a better wording
for it.
Currently, when you use namespaces in PHP, whenever you want to call a
function from inside a namespace you must either use the entire name of the
namespace (potentially very long) or give it a shorter alias.
Here is an example:
namespace.inc
namespace ProjectA;
const WORD = 'foobar';
execute.php
use ProjectA as A;
print A::WORD;
This works out fine. But when I change the two last lines in execute.php to:
use ProjectA;
print WORD;
I get a warning saying "The use statement with non-compound name 'ProjectA'
has no effect ..." and naturally also a notice about using a non-defined
constant.
So, the default namespace in use, is always global. What about if the default
could be changed? It would put to rest the need to always specify the
namespace from which to look for the function or class. This would mean that
the last example of execute.php would be valid. You could also still access
the global space with the following syntax:
execute.php
const BOOK = 'Two Towers';
use ProjectA;
print ::BOOK;
Any thoughts on this? Was this sort of approach considered when the namespaces
were being implemented?
Tomi Kaistila
PHP Developer
"use ProjectA;" is equivalent of "use ProjectA as ProjectA;"
if you want to use something from namespace you should "use" that something.
for example: "use ProjectA::BOOK;"
Sorry for the ambiguous subject line, but I could not find a better wording
for it.Currently, when you use namespaces in PHP, whenever you want to call a
function from inside a namespace you must either use the entire name of the
namespace (potentially very long) or give it a shorter alias.Here is an example:
namespace.inc
namespace ProjectA;
const WORD = 'foobar';execute.php
use ProjectA as A;
print A::WORD;This works out fine. But when I change the two last lines in execute.php to:
use ProjectA;
print WORD;I get a warning saying "The use statement with non-compound name 'ProjectA'
has no effect ..." and naturally also a notice about using a non-defined
constant.So, the default namespace in use, is always global. What about if the default
could be changed? It would put to rest the need to always specify the
namespace from which to look for the function or class. This would mean that
the last example of execute.php would be valid. You could also still access
the global space with the following syntax:execute.php
const BOOK = 'Two Towers';
use ProjectA;
print ::BOOK;Any thoughts on this? Was this sort of approach considered when the namespaces
were being implemented?Tomi Kaistila
PHP Developer--
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
Yes, I know. My suggestion/inquiry was about changing this behavior. So
instead of "use ProjectA;" being equivalent to "use ProjectA as ProjectA;" by
leaving the "as Something" out the default namespace would be changed.
So by calling "use ProjectA;" the default namespace would change from global
to ProjectA. This would be useful especially with small applications or when
(for whatever reason) you do not want to maintain a wide range of namespaces.
Say you had ProjectA as the main namespace and then within it you would have 5
packages with an arbitrary number of classes in each. You could choose to give
each package a namespace, within the ProjectA namespace, or you could just as
easily just have them all as part of the ProjectA namespace. This alone
already lessens the possibility of name collisions and indeed having lots of
namespaces can get unnecessarily confusing. After all, in PHP, the concept
of "packages" only exists in documentation.
Tomi Kaistila
PHP Developer
On Thursday 10 January 2008 11:42:29 you wrote:
"use ProjectA;" is equivalent of "use ProjectA as ProjectA;"
if you want to use something from namespace you should "use" that
something.for example: "use ProjectA::BOOK;"
Sorry for the ambiguous subject line, but I could not find a better
wording for it.Currently, when you use namespaces in PHP, whenever you want to call a
function from inside a namespace you must either use the entire name of
the namespace (potentially very long) or give it a shorter alias.Here is an example:
namespace.inc
namespace ProjectA;
const WORD = 'foobar';execute.php
use ProjectA as A;
print A::WORD;This works out fine. But when I change the two last lines in execute.php
to:use ProjectA;
print WORD;I get a warning saying "The use statement with non-compound name
'ProjectA' has no effect ..." and naturally also a notice about using a
non-defined constant.So, the default namespace in use, is always global. What about if the
default could be changed? It would put to rest the need to always specify
the namespace from which to look for the function or class. This would
mean that the last example of execute.php would be valid. You could also
still access the global space with the following syntax:execute.php
const BOOK = 'Two Towers';
use ProjectA;
print ::BOOK;Any thoughts on this? Was this sort of approach considered when the
namespaces were being implemented?Tomi Kaistila
PHP Developer