Hi,
I was just using the PHP namespaces for the first time and noticed a
difference that i really didn't expect. (No, i won't start complaining
about the slash based namespace).
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.
In PHP that's a bit different. Lets take this as an example:
namespacetst.php
<?php
namespace Some\Long\Namespace;
Class SomeClass
{
}
Now the one using that "SomeClass" has to do something like this:
index.php
use Some\Long\Namespace;
$oClass = new Namespace\SomeClass();
Yes, you can also do:
index.php
use Some\Long\Namespace\SomeClass;
$oClass = new SomeClass();
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.
I hope someone can shed some light over this.
Regards,
Mark
Hi,
I was just using the PHP namespaces for the first time and noticed a
difference that i really didn't expect. (No, i won't start complaining
about the slash based namespace).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.In PHP that's a bit different. Lets take this as an example:
namespacetst.php
<?php
namespace Some\Long\Namespace;Class SomeClass
{
}Now the one using that "SomeClass" has to do something like this:
index.php
use Some\Long\Namespace;$oClass = new Namespace\SomeClass();
Yes, you can also do:
index.php
use Some\Long\Namespace\SomeClass;
$oClass = new SomeClass();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.I hope someone can shed some light over this.
Regards,
Mark--
Yes, PHP namespaces are completely different from what you'd be used
to in C++. In all honesty namespaces were never well designed in PHP
and were implemented in a haphazard way, which is why I generally
don't bother using them.
To clarify, importing namespaces in PHP isn't like importing
namespaces in C++ at all, really. You are merely aliasing namespaces
in PHP when you use the "use" keyword. Meaning that what's actually
happening is PHP expects you to alias one namespace to another (and
thus you can never import one namespace directly into the existing
namespace since this creates a name conflict). It's messy...
NameSpacedFile.php
namespace My\Name\Space;
class MyClass { }
Index.php
use My\Name\Space; // This doesn't actually import anything
// What happened here is we created an alias
// It's the same thing as saying "use My\Name\Space as \Space
// So now My\Name\Space is aliased to \Space
$obj = new \Space\MyClass; // great
$obj = new MyClass; // this won't do what you want
Here's the full example:
It simply doesn't work like that because PHP's namespaces are
implemented in such a way that they don't really resemble namespaces.
Just some fancy magic going on in the engine that allow it to mimic
namespaces.
Hi,
I was just using the PHP namespaces for the first time and noticed a
difference that i really didn't expect. (No, i won't start complaining
about the slash based namespace).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.In PHP that's a bit different. Lets take this as an example:
namespacetst.php
<?php
namespace Some\Long\Namespace;Class SomeClass
{
}Now the one using that "SomeClass" has to do something like this:
index.php
use Some\Long\Namespace;$oClass = new Namespace\SomeClass();
Yes, you can also do:
index.php
use Some\Long\Namespace\SomeClass;
$oClass = new SomeClass();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.I hope someone can shed some light over this.
Regards,
Mark--
Yes, PHP namespaces are completely different from what you'd be used
to in C++. In all honesty namespaces were never well designed in PHP
and were implemented in a haphazard way, which is why I generally
don't bother using them.To clarify, importing namespaces in PHP isn't like importing
namespaces in C++ at all, really. You are merely aliasing namespaces
in PHP when you use the "use" keyword. Meaning that what's actually
happening is PHP expects you to alias one namespace to another (and
thus you can never import one namespace directly into the existing
namespace since this creates a name conflict). It's messy...NameSpacedFile.php
namespace My\Name\Space;
class MyClass { }Index.php
use My\Name\Space; // This doesn't actually import anything
// What happened here is we created an alias
// It's the same thing as saying "use My\Name\Space as \Space
// So now My\Name\Space is aliased to \Space$obj = new \Space\MyClass; // great
$obj = new MyClass; // this won't do what you wantHere's the full example:
It simply doesn't work like that because PHP's namespaces are
implemented in such a way that they don't really resemble namespaces.
Just some fancy magic going on in the engine that allow it to mimic
namespaces.
While that is true, I cannot stress their importance in organizing
code. Nearly every codebase I've seen that can use namespaces maps
the namespace to the file system which makes for very easy-to-code
autoloaders. Very helpful indeed. It would be nice to see improved
namespaces, though.
On Fri, Sep 7, 2012 at 3:21 AM, Levi Morrison morrison.levi@gmail.comwrote:
On Thu, Sep 6, 2012 at 6:15 PM, Sherif Ramadan theanomaly.is@gmail.com
wrote:Hi,
I was just using the PHP namespaces for the first time and noticed a
difference that i really didn't expect. (No, i won't start complaining
about the slash based namespace).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.In PHP that's a bit different. Lets take this as an example:
namespacetst.php
<?php
namespace Some\Long\Namespace;Class SomeClass
{
}Now the one using that "SomeClass" has to do something like this:
index.php
use Some\Long\Namespace;$oClass = new Namespace\SomeClass();
Yes, you can also do:
index.php
use Some\Long\Namespace\SomeClass;
$oClass = new SomeClass();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.I hope someone can shed some light over this.
Regards,
Mark--
Yes, PHP namespaces are completely different from what you'd be used
to in C++. In all honesty namespaces were never well designed in PHP
and were implemented in a haphazard way, which is why I generally
don't bother using them.To clarify, importing namespaces in PHP isn't like importing
namespaces in C++ at all, really. You are merely aliasing namespaces
in PHP when you use the "use" keyword. Meaning that what's actually
happening is PHP expects you to alias one namespace to another (and
thus you can never import one namespace directly into the existing
namespace since this creates a name conflict). It's messy...NameSpacedFile.php
namespace My\Name\Space;
class MyClass { }Index.php
use My\Name\Space; // This doesn't actually import anything
// What happened here is we created an alias
// It's the same thing as saying "use My\Name\Space as \Space
// So now My\Name\Space is aliased to \Space$obj = new \Space\MyClass; // great
$obj = new MyClass; // this won't do what you wantHere's the full example:
It simply doesn't work like that because PHP's namespaces are
implemented in such a way that they don't really resemble namespaces.
Just some fancy magic going on in the engine that allow it to mimic
namespaces.While that is true, I cannot stress their importance in organizing
code. Nearly every codebase I've seen that can use namespaces maps
the namespace to the file system which makes for very easy-to-code
autoloaders. Very helpful indeed. It would be nice to see improved
namespaces, though.--
Will the current implementation of namespace designed for import all
classes, functions and such from namespace? or it should be rewritten
entirely in order to support that?
How about adding ability to import the entire namespace?
Hi!
How about adding ability to import the entire namespace?
This option was explicitly excluded because this defeats the whole idea
of having namespaces - i.e. having names to live in different spaces -
by explicitly bringing unknown set of names into global space. If you
allow global import, then you do not know which any of the names comes
from - it can come from any of the set of imported namespaces, or be
just global - and you have absolutely no idea about it and no way to
avoid conflicts. And all that to save you three keystrokes so you could
write DbConnection and not Db\DbConnection? Definitely not a good idea.
Current implementation makes it clear where things come from and avoids
naming conflicts, while still allowing you to use short names instead of
long and painful ones.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
On Fri, Sep 7, 2012 at 3:41 AM, Stas Malyshev smalyshev@sugarcrm.comwrote:
Hi!
How about adding ability to import the entire namespace?
This option was explicitly excluded because this defeats the whole idea
of having namespaces - i.e. having names to live in different spaces -
by explicitly bringing unknown set of names into global space. If you
allow global import, then you do not know which any of the names comes
from - it can come from any of the set of imported namespaces, or be
just global - and you have absolutely no idea about it and no way to
avoid conflicts. And all that to save you three keystrokes so you could
write DbConnection and not Db\DbConnection? Definitely not a good idea.
Current implementation makes it clear where things come from and avoids
naming conflicts, while still allowing you to use short names instead of
long and painful ones.Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
That's true, but we do got the ability to import only one class from given
namespace and classes aliasing so we can, for example, do something like:
namespaceClasses.php
namespace MyNamespace;
class Foo { }
class Bar { }
class Baz { }
use \MyNamespace* {
Foo as F
Bar as B
};
Then - the Foo and Bar classes will be F and B while Baz and any other
class remain Baz.
I do understand the point of conflicts, but I think that we should give the
programmer the ability to decide whether to import specific class or the
entire namespace.
Other option I've thought of is to declare rules for importing as keywords
or even a function that can import the namespace with given conflict rules
for example
use \MyNamespace* noconflict;
or even
import_namespace('\MyNamespace', NS_AVOID_CONFICT);
or something like that, though I think that a keyword allocated only to
that propose is definitely not good approach...
Hi!
That's true, but we do got the ability to import only one class from
given namespace and classes aliasing so we can, for example, do
something like:
When you import one name, you see this name in import statement. It's
explicit. Global imports are not.
I really hate to rehash discussion from years ago all from the start.
Please believe we did think about it - including having special options,
functions, configurations, INI entries, etc. for defining imports. It's
not worth it to save couple of keystrokes on namespace prefix, really.
Not everything should live in global space, that's the whole point of
namespaces.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi,
On Fri, Sep 7, 2012 at 3:41 AM, Stas Malyshev smalyshev@sugarcrm.comwrote:
Hi!
How about adding ability to import the entire namespace?
This option was explicitly excluded because this defeats the whole idea
of having namespaces - i.e. having names to live in different spaces -
by explicitly bringing unknown set of names into global space. If you
allow global import, then you do not know which any of the names comes
from - it can come from any of the set of imported namespaces, or be
just global - and you have absolutely no idea about it and no way to
avoid conflicts. And all that to save you three keystrokes so you could
write DbConnection and not Db\DbConnection? Definitely not a good idea.
Current implementation makes it clear where things come from and avoids
naming conflicts, while still allowing you to use short names instead of
long and painful ones.Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227That's true, but we do got the ability to import only one class from given
namespace and classes aliasing so we can, for example, do something like:namespaceClasses.php
namespace MyNamespace;
class Foo { }
class Bar { }
class Baz { }use \MyNamespace* {
Foo as F
Bar as B
};Then - the Foo and Bar classes will be F and B while Baz and any other
class remain Baz.
I do understand the point of conflicts, but I think that we should give the
programmer the ability to decide whether to import specific class or the
entire namespace.Other option I've thought of is to declare rules for importing as keywords
or even a function that can import the namespace with given conflict rulesfor example
use \MyNamespace* noconflict;
or even
import_namespace('\MyNamespace', NS_AVOID_CONFICT);or something like that, though I think that a keyword allocated only to
that propose is definitely not good approach...
The reason why this is not implemented is simple, and it's not to
avoid hard-to-read code or "by design". It is a purely technical
limitation of the way PHP handles classes/namespaces:
When importing a namespace, PHP may have no idea of the classes
contained in that namespace. So when you do
use Foo\Bar as Gee;
PHP have no idea what Foo\Bee really is, whether it is a namespace
itself or a class. Nothing might be loaded at that time. All it
knows is that it needs to substitute occurrences of "Gee" with
"Foo\Bar", that's it.
Now of course, this wouldn't work with wildcard imports, because if you do:
use Foo*;
use Bar*;
new Plop;
Even though Plop might only be contained in one of those namespaces,
PHP does not necessarily know, and it doesn't know whether to try
\Plop, Foo\Plop, or Bar\Plop.
This problem is of course non-existent in other languages supporting
wildcard imports, because the set of imported classes is finite and
well known, which means that conflicts can be detected right away.
Best,
--
Etienne Kneuss
Hi!
Yes, PHP namespaces are completely different from what you'd be used
to in C++. In all honesty namespaces were never well designed in PHP
and were implemented in a haphazard way, which is why I generally
don't bother using them.
I just love how people assume if something does not fit their specific
use case people implementing it must be stupid and didn't bother to
actually think about it. If only they gave it some though, they would
definitely would do it exactly as you want.
To clarify, importing namespaces in PHP isn't like importing
namespaces in C++ at all, really. You are merely aliasing namespaces
There's no importing namespaces in PHP, period. It is not "messy", it is
by design, and if you bothered to read extensive discussions that
happened on the list at the time, you'd know why. But of course who
needs that.
It simply doesn't work like that because PHP's namespaces are
implemented in such a way that they don't really resemble namespaces.
They do not resemble namespaces in C++. They resemble namespaces in PHP.
That's because PHP is not C++.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
Yes, PHP namespaces are completely different from what you'd be used
to in C++. In all honesty namespaces were never well designed in PHP
and were implemented in a haphazard way, which is why I generally
don't bother using them.I just love how people assume if something does not fit their specific
use case people implementing it must be stupid and didn't bother to
actually think about it. If only they gave it some though, they would
definitely would do it exactly as you want.
I wasn't assuming. I was outright making a factual statement. I never
made any implications of the intellectual levels of those implementing
the spec. I understand the RFC full well and know why the design is
the way it is. I was answering the ops question. Please read what I
said before you make your own assumptions.
To clarify, importing namespaces in PHP isn't like importing
namespaces in C++ at all, really. You are merely aliasing namespacesThere's no importing namespaces in PHP, period. It is not "messy", it is
by design, and if you bothered to read extensive discussions that
happened on the list at the time, you'd know why. But of course who
needs that.
Right, that's exactly what I said. Thanks for reiterating.
It simply doesn't work like that because PHP's namespaces are
implemented in such a way that they don't really resemble namespaces.They do not resemble namespaces in C++. They resemble namespaces in PHP.
That's because PHP is not C++.
Sure, I just said that. Albeit I said it a more indirect manner.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
I wasn't assuming. I was outright making a factual statement. I never
made any implications of the intellectual levels of those implementing
the spec. I understand the RFC full well and know why the design is
the way it is. I was answering the ops question. Please read what I
said before you make your own assumptions.
Sorry, statements like "haphazard way", "never well designed", "it's a
mess", "they don't really resemble namespaces", "just some fancy magic",
etc. have nothing to do with facts. Actually, facts are exactly the
opposite - they were designed, were extensively discussed with
soliciting feedback from many stakeholders, and were implemented exactly
as planned. You may not like the way there were implemented, that's your
opinion (not a fact) and you are entitled to it. But you didn't limit
yourself to saying "I don't like them". You specifically said that they
were never well designed and haphazardly implemented. This is factually
false.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
I wasn't assuming. I was outright making a factual statement. I never
made any implications of the intellectual levels of those implementing
the spec. I understand the RFC full well and know why the design is
the way it is. I was answering the ops question. Please read what I
said before you make your own assumptions.Sorry, statements like "haphazard way", "never well designed", "it's a
mess", "they don't really resemble namespaces", "just some fancy magic",
etc. have nothing to do with facts. Actually, facts are exactly the
opposite - they were designed, were extensively discussed with
soliciting feedback from many stakeholders, and were implemented exactly
as planned. You may not like the way there were implemented, that's your
opinion (not a fact) and you are entitled to it. But you didn't limit
yourself to saying "I don't like them". You specifically said that they
were never well designed and haphazardly implemented. This is factually
false.
Fair enough. I'll be more careful to separate my opinions from my
objective answers next time :)
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Stas Malyshev wrote:
Hi!
I wasn't assuming. I was outright making a factual statement. I never
made any implications of the intellectual levels of those implementing
the spec. I understand the RFC full well and know why the design is
the way it is. I was answering the ops question. Please read what I
said before you make your own assumptions.Sorry, statements like "haphazard way", "never well designed", "it's a
mess", "they don't really resemble namespaces", "just some fancy magic",
etc. have nothing to do with facts. Actually, facts are exactly the
opposite - they were designed, were extensively discussed with
soliciting feedback from many stakeholders, and were implemented exactly
as planned. You may not like the way there were implemented, that's your
opinion (not a fact) and you are entitled to it. But you didn't limit
yourself to saying "I don't like them". You specifically said that they
were never well designed and haphazardly implemented. This is factually
false.
Stas ... One thing to bear in mind is that even for those of us for whom English
is our only language it is sometimes difficult to explain what we mean. Sherif's
were perhaps a little 'provocative' but were an accurate reflection of his view
on the results of implementing namespaces. It is a compromise rather than
something that sits naturally in PHP?
What a lot of newcomers need to understand is that PHP is NOT compiled, so a
heck of a lot of what they are used to just happening is physically impossible,
and namespace is a good case in point. Personally I have yet to see a good
example of the use of namespaces in third party libraries, and like Sherif avoid
them preferring simply so that while the code may be more verbose, it's clear 5
years later what was intended :) A lot of the current 'magic' makes it difficult
to pick up and work with other peoples code later on.
--
Lester Caine - G8HFL
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
Stas Malyshev wrote:
Hi!
I wasn't assuming. I was outright making a factual statement. I never
made any implications of the intellectual levels of those implementing
the spec. I understand the RFC full well and know why the design is
the way it is. I was answering the ops question. Please read what I
said before you make your own assumptions.Sorry, statements like "haphazard way", "never well designed", "it's a
mess", "they don't really resemble namespaces", "just some fancy magic",
etc. have nothing to do with facts. Actually, facts are exactly the
opposite - they were designed, were extensively discussed with
soliciting feedback from many stakeholders, and were implemented exactly
as planned. You may not like the way there were implemented, that's your
opinion (not a fact) and you are entitled to it. But you didn't limit
yourself to saying "I don't like them". You specifically said that they
were never well designed and haphazardly implemented. This is factually
false.Stas ... One thing to bear in mind is that even for those of us for whom
English is our only language it is sometimes difficult to explain what we
mean. Sherif's were perhaps a little 'provocative' but were an accurate
reflection of his view on the results of implementing namespaces. It is a
compromise rather than something that sits naturally in PHP?What a lot of newcomers need to understand is that PHP is NOT compiled, so a
heck of a lot of what they are used to just happening is physically
impossible, and namespace is a good case in point. Personally I have yet to
see a good example of the use of namespaces in third party libraries, and
like Sherif avoid them preferring simply so that while the code may be more
verbose, it's clear 5 years later what was intended :) A lot of the current
'magic' makes it difficult to pick up and work with other peoples code later
on.
Right, i have to correct you there.
You don't explicitly say it, but you do think that i'm a "newcommer".
That's far from the truth. I've been using PHP since 2001 and i
consider myself to be quite advanced in PHP usage.
Back on the namespace topic. I won't judge on anything, but i know
namespaces from c++. I understand that PHP isn't a compiled language
and quite frankly that doesn't matter at all. What does matter is that
PHP uses the name: "namespace" which C++ obviously had earlier.
Naturally someone that knows both languaes (c++ and php) simply assume
that a namespace in php probably works the same as those in C++. If
that's not the case then it shouldn't even carry the "namespace" name.
To me this namespace in php stuff just looks like and alias. So why
isn't it named as that: "alias"? I do not want to question the ones
that implemented namespaces in php, but i do think that the name
itself is at the very least confusing.
Also, lets keep this discussion polite people. I'm not here to smash
toes. All i would like to know is the reasoning behind the above and
if there is any intention of changing the namespaces to be more c++
like.
Cheers,
Mark
To me this namespace in php stuff just looks like and alias. So why
isn't it named as that: "alias"? I do not want to question the ones
that implemented namespaces in php, but i do think that the name
itself is at the very least confusing.
Namespaces, like in C++, mean you can reference classes etc from other
modules without name conflicts, yet not have to type gl_ or zend_ or
whatever in front of every single name when writing code within that
module, and that's about it.
Yes, the behaviour is different, but they are still namespaces. Them
being different from C++'s shouldn't be a surprise, C++ is quite a
different language.
--
Andrew Faulds
http://ajf.me/
Stas Malyshev wrote:
Hi!
I wasn't assuming. I was outright making a factual statement. I never
made any implications of the intellectual levels of those implementing
the spec. I understand the RFC full well and know why the design is
the way it is. I was answering the ops question. Please read what I
said before you make your own assumptions.Sorry, statements like "haphazard way", "never well designed", "it's a
mess", "they don't really resemble namespaces", "just some fancy magic",
etc. have nothing to do with facts. Actually, facts are exactly the
opposite - they were designed, were extensively discussed with
soliciting feedback from many stakeholders, and were implemented exactly
as planned. You may not like the way there were implemented, that's your
opinion (not a fact) and you are entitled to it. But you didn't limit
yourself to saying "I don't like them". You specifically said that they
were never well designed and haphazardly implemented. This is factually
false.Stas ... One thing to bear in mind is that even for those of us for whom
English is our only language it is sometimes difficult to explain what we
mean. Sherif's were perhaps a little 'provocative' but were an accurate
reflection of his view on the results of implementing namespaces. It is a
compromise rather than something that sits naturally in PHP?What a lot of newcomers need to understand is that PHP is NOT compiled,
so a
heck of a lot of what they are used to just happening is physically
impossible, and namespace is a good case in point. Personally I have yet
to
see a good example of the use of namespaces in third party libraries, and
like Sherif avoid them preferring simply so that while the code may be
more
verbose, it's clear 5 years later what was intended :) A lot of the
current
'magic' makes it difficult to pick up and work with other peoples code
later
on.Right, i have to correct you there.
You don't explicitly say it, but you do think that i'm a "newcommer".
That's far from the truth. I've been using PHP since 2001 and i
consider myself to be quite advanced in PHP usage.Back on the namespace topic. I won't judge on anything, but i know
namespaces from c++. I understand that PHP isn't a compiled language
and quite frankly that doesn't matter at all. What does matter is that
PHP uses the name: "namespace" which C++ obviously had earlier.
Naturally someone that knows both languaes (c++ and php) simply assume
that a namespace in php probably works the same as those in C++. If
that's not the case then it shouldn't even carry the "namespace" name.
Right, I have to correct you there. Just because a namespace doesn't act
like C++'s namespace implementation doesn't mean it's not a namespace. A
namespace is just an abstract container which groups unique symbols
(names). The PHP implementation does exactly that.
To me this namespace in php stuff just looks like and alias. So why
isn't it named as that: "alias"? I do not want to question the ones
that implemented namespaces in php, but i do think that the name
itself is at the very least confusing.
Because it behaves as a namespace. Again, just because the implementation
isn't what you're used to, or even like, it still groups identifiers in
abstract containers. I realize that the implementation and ZE backend may
cause some confusion with 'aliasing', but it's use case is the same.
Also note:
use Some\Ns\Class as MyClass;
There's an alias for you. ;)
Also, lets keep this discussion polite people. I'm not here to smash
toes. All i would like to know is the reasoning behind the above and
if there is any intention of changing the namespaces to be more c++
like.Cheers,
Mark
Stas Malyshev wrote:
Hi!
I wasn't assuming. I was outright making a factual statement. I never
made any implications of the intellectual levels of those implementing
the spec. I understand the RFC full well and know why the design is
the way it is. I was answering the ops question. Please read what I
said before you make your own assumptions.Sorry, statements like "haphazard way", "never well designed", "it's a
mess", "they don't really resemble namespaces", "just some fancy magic",
etc. have nothing to do with facts. Actually, facts are exactly the
opposite - they were designed, were extensively discussed with
soliciting feedback from many stakeholders, and were implemented exactly
as planned. You may not like the way there were implemented, that's your
opinion (not a fact) and you are entitled to it. But you didn't limit
yourself to saying "I don't like them". You specifically said that they
were never well designed and haphazardly implemented. This is factually
false.Stas ... One thing to bear in mind is that even for those of us for whom
English is our only language it is sometimes difficult to explain what we
mean. Sherif's were perhaps a little 'provocative' but were an accurate
reflection of his view on the results of implementing namespaces. It is a
compromise rather than something that sits naturally in PHP?What a lot of newcomers need to understand is that PHP is NOT compiled,
so a
heck of a lot of what they are used to just happening is physically
impossible, and namespace is a good case in point. Personally I have yet
to
see a good example of the use of namespaces in third party libraries, and
like Sherif avoid them preferring simply so that while the code may be
more
verbose, it's clear 5 years later what was intended :) A lot of the
current
'magic' makes it difficult to pick up and work with other peoples code
later
on.Right, i have to correct you there.
You don't explicitly say it, but you do think that i'm a "newcommer".
That's far from the truth. I've been using PHP since 2001 and i
consider myself to be quite advanced in PHP usage.Back on the namespace topic. I won't judge on anything, but i know
namespaces from c++. I understand that PHP isn't a compiled language
and quite frankly that doesn't matter at all. What does matter is that
PHP uses the name: "namespace" which C++ obviously had earlier.
Naturally someone that knows both languaes (c++ and php) simply assume
that a namespace in php probably works the same as those in C++. If
that's not the case then it shouldn't even carry the "namespace" name.To me this namespace in php stuff just looks like and alias. So why
isn't it named as that: "alias"? I do not want to question the ones
that implemented namespaces in php, but i do think that the name
itself is at the very least confusing.Also, lets keep this discussion polite people. I'm not here to smash
toes. All i would like to know is the reasoning behind the above and
if there is any intention of changing the namespaces to be more c++
like.
Also forgot to throw this in:
http://marc.info/?r=1&q=b&l=php-internals&s=namespace+implementation&w=3.
This topic was discussed over a long period of time, and if you truly want
to understand the logic behind it, please read all the threads.
Cheers,
Mark
Hi!
Back on the namespace topic. I won't judge on anything, but i know
namespaces from c++. I understand that PHP isn't a compiled language
and quite frankly that doesn't matter at all. What does matter is that
PHP uses the name: "namespace" which C++ obviously had earlier.
Here is something that I would like to emphasize. There's no "one right
way" to do namespacing. And while PHP borrows a lot from other
languages, it was never promised or guaranteed that PHP would work
exactly like C++ or Java or Python or Ruby or any other language. If it
makes sense for PHP, it is taken in. If it's not, it is not taken.
In the case of namespaces, we needed to solve a particular problem of
PHP - global namespace pollution and people inventing super-long really
tedious names to deal with it. Moreover, it is in general exactly what
namespaces are solving in each language. Namespaces were designed and
implemented to solve this particular problems, while keeping all other
parts of PHP working properly.
Naturally someone that knows both languaes (c++ and php) simply assume
that a namespace in php probably works the same as those in C++. If
that's not the case then it shouldn't even carry the "namespace" name.
I couldn't disagree more. Namespace is a generic term, not belonging to
C++. PHP is not some offshoot of C++ that is required either behave like
C++ or stay away from it. Again, if it made sense in PHP to implement
generic concept of namespaces in a particular way that C++ does it -
we might do it. But it didn't, so we didn't. We have no responsibility
and no obligation to make implementations of generic concepts match
those in other languages or invent awkward names for those if they do
not. There's a value in behaving like other languages, but it only goes
so far.
Namespaces in PHP are as valid implementation of namespacing as those in
C++, they are just different. You came to it with prejudices about how
they should be working, these prejudices proved to be wrong, now you
know more and you continue to be more productive. This is natural and
happens to many people (myself included) quite often - one assumes
something, one is sometimes right and sometimes wrong. If one is wrong,
one corrects the mistake and becomes wiser. However I can't understand
how it makes sense to blame the language for not matching somebody's
personal background and prejudices.
To me this namespace in php stuff just looks like and alias. So why
isn't it named as that: "alias"? I do not want to question the ones
that implemented namespaces in php, but i do think that the name
itself is at the very least confusing.
It's not an alias. Aliasing is a part of it, but not the whole.
Declaration and the beginning of the file saying "alias My\Database"
makes little sense, declaration "namespace My\Database" makes great
sense - it shows that the file belongs to namespace My\Database, with
all consequences of that.
Also, lets keep this discussion polite people. I'm not here to smash
toes. All i would like to know is the reasoning behind the above and
I think I have explained the reasoning - specifically, why we have no
global imports - in my previous emails, but if you still have something
unclear, you are welcome to ask. It would make me happier though if you
tried to read through the archives where namespaces were discussed and
see all those arguments so we won't have to repeat them again, even
though I understand that nobody really does that :)
if there is any intention of changing the namespaces to be more c++
like.
I would evaluate the chances of it as very small and I would be in a
very vocal and active opposition to that.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
Back on the namespace topic. I won't judge on anything, but i know
namespaces from c++. I understand that PHP isn't a compiled language
and quite frankly that doesn't matter at all. What does matter is that
PHP uses the name: "namespace" which C++ obviously had earlier.Here is something that I would like to emphasize. There's no "one right
way" to do namespacing. And while PHP borrows a lot from other
languages, it was never promised or guaranteed that PHP would work
exactly like C++ or Java or Python or Ruby or any other language. If it
makes sense for PHP, it is taken in. If it's not, it is not taken.In the case of namespaces, we needed to solve a particular problem of
PHP - global namespace pollution and people inventing super-long really
tedious names to deal with it. Moreover, it is in general exactly what
namespaces are solving in each language. Namespaces were designed and
implemented to solve this particular problems, while keeping all other
parts of PHP working properly.Naturally someone that knows both languaes (c++ and php) simply assume
that a namespace in php probably works the same as those in C++. If
that's not the case then it shouldn't even carry the "namespace" name.I couldn't disagree more. Namespace is a generic term, not belonging to
C++. PHP is not some offshoot of C++ that is required either behave like
C++ or stay away from it. Again, if it made sense in PHP to implement
generic concept of namespaces in a particular way that C++ does it -
we might do it. But it didn't, so we didn't. We have no responsibility
and no obligation to make implementations of generic concepts match
those in other languages or invent awkward names for those if they do
not. There's a value in behaving like other languages, but it only goes
so far.
Namespaces in PHP are as valid implementation of namespacing as those in
C++, they are just different. You came to it with prejudices about how
they should be working, these prejudices proved to be wrong, now you
know more and you continue to be more productive. This is natural and
happens to many people (myself included) quite often - one assumes
something, one is sometimes right and sometimes wrong. If one is wrong,
one corrects the mistake and becomes wiser. However I can't understand
how it makes sense to blame the language for not matching somebody's
personal background and prejudices.To me this namespace in php stuff just looks like and alias. So why
isn't it named as that: "alias"? I do not want to question the ones
that implemented namespaces in php, but i do think that the name
itself is at the very least confusing.It's not an alias. Aliasing is a part of it, but not the whole.
Declaration and the beginning of the file saying "alias My\Database"
makes little sense, declaration "namespace My\Database" makes great
sense - it shows that the file belongs to namespace My\Database, with
all consequences of that.Also, lets keep this discussion polite people. I'm not here to smash
toes. All i would like to know is the reasoning behind the above andI think I have explained the reasoning - specifically, why we have no
global imports - in my previous emails, but if you still have something
unclear, you are welcome to ask. It would make me happier though if you
tried to read through the archives where namespaces were discussed and
see all those arguments so we won't have to repeat them again, even
though I understand that nobody really does that :)if there is any intention of changing the namespaces to be more c++
like.I would evaluate the chances of it as very small and I would be in a
very vocal and active opposition to that.Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Oke, it's clear to me now. I understand why PHP decided to do it this
way (preventing global scope pollution). I don't agree on that, but
it's nice to know the reasoning behind it. Guess i just have to live
with it ^_
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