The concept of Namespaces provides a way to help avoid problems with multiple functions, classes, and constants of the same name being defined multiple times. PHP 5.3 borrows much of the syntax and design of Namespaces from other languages ^aEUR” most notably C++. However, it does deal with Namespaces in a somewhat unique way, which may cause problems for those familiar with Namespaces. This article takes a close look at PHP V5.3 namespaces, which is one of the most anticipated and the most debated feature in this release of PHP.
Worst syntax ever!
php is a ‘curly brace’ language. thus the obvious implementation of namespaces would be:
namespace Foo
{
Class Bar
{
}
}
with the obvious seperators being the dot
eg. Foo.Bar
The syntax seems like they just thought, “damn we already look too much like Java,C++,Perl so lets take some python syntax and create some syntax that’s completely different because otherwise people will just think we’re trying to be just like Java”
Yeah, the syntax for the namespace stinks. Although, I’m glad they did something different to differentiate between a namespace and a class/function than they did between a class and a function.
namespace Foo;
Class Bar{
public function Foo() {
print “I’m fooed up\n”;
}
}
Then in another section of the code ..
$newOject = Foo\Bar();
$newObject->Foo();
Rather than what they could have done by not making them distinct
$newObject= Foo->Bar();
$newObject->Foo();
Which is basically what Java does, uses the period for namespace (or package) separation and method separation. Using a period in PHP would confuse the heck out of me as I use both languages, and forget that Foo.bar was a namespace resolution instead of a method.
But, I am also confused why they used both \ and / in the article as theosib pointed out.
Edited 2009-01-16 16:51 UTC
While for aesthetic reasons I would prefer curly braces style, this syntax was probably adopted for ease of migrating existing code.
The common use case to convert old code would be to insert a “namespace” keyword line and perhaps remove a prefix from class names. A ripple of indentation would require more work and cause spurious diffs in version management tools. So this decision has a lot of merit.
I also wept at the backslash, but given the resources available, what can you do? All the other alternatives (you can read the RFC) had serious drawbacks or were a lot uglier. As an advantage, I think “\globalFunction()” to reference a function in the global namespace looks a lot cleaner than “::globalFunction()” and I think many novice users will find it intuitive, as you can immediately recognize it as “the real” globalFunction or something living under the “root node” in a hierarchy.
We’ll get used to it and we’ll appreciate the highly valuable language additions.
This article makes inconsistent use of / and \. Which is it? Or do both work? It’s one thing when the idiot phone tech support person tells me to put backslashes in a URL. At least I can just say ‘uh-huh’ and type the right thing. But in any case, the distinction is critical, and this author didn’t even bother to mention it (unless I just missed it, although I did read the article).
I think it’s funny how people hate on c++ so much, but as time goes on more and more languages add features that have been in c++ for decades. (think we-dont-need-no-templates java and then woah whats that generics?)
And yet another fudged up piece of the spaghetti mess that PHP is…
Why anybody would expect anything else from a language with a credo like: “borrow if possible, invent if necessary”
I have come to read that as: “copy/paste if possible, kludge/hack if necessary”
Wow, I wouldn’t have even give them that much credit, and I sorta like the language. I’d read it as “copy/paste sometimes, other times for no good reason kludge/hack”.
Its sort of like the English language in a lot of ways. At a certain point you achieve a fluency and you don’t have to think about how inconstant or messed up the syntax and rules are. Yeah, it would be nice if it wasn’t so messed up, but its almost like its default on the web.
The difference is that English actually works and is reasonably fast and can be used to write some beautiful things. I don’t those are generally true for PHP
I can’t wait to get a job where I don’t have to do PHP. I actually prefer JavaScript to PHP…
Here’s the way I see it.
C/C++ = the most useless trash language around.
C# = horrible copy of java.
Java = Worthless.
Javascript = stupid version of Java for browser.
HTML = Stoopud.
Assembly = the easiest language to learn.
PHP = Best Damn programming language to date.
CSS = Easy to use language.
Visual Basic (prior to dot puke) = Awesome language.
(I kid of course.)
It never ceases to amaze me that every time there’s a PHP article on OS news, a bunch of people come into the thread who clearly don’t like PHP, with the sole intention of bad mouthing it rather than contributing to any useful conversation. It would be nice, if just for once, people stayed on topic.
At first I thought the back slash was a bit ugly, but really it’s no different from what some file systems use to separate levels of folders.
Folder\File
Namespace\Function
I think we just need to get used to it.
The problem is that the convention does not fit within PHP. In PHP, backslash has always meant escape character, as it does in many other C-like languages. To separate components of objects and such, you always use either :: or ->. This is also the convention in other languages. So when it’s time to choose the separator for namespaces, the PHP developers, as usual, go and pick the one WRONG way to do it and go with the backslash, which makes no sense in any convention and looks ugly to boot (Windows is the only OS which stupidly uses backslashes as path separators anyways, that’s hardly a system I’d want to follow).
There’s a reason people badmouth PHP. Actually, a lot of reasons. The developers consistently eschew solid design and convention in favor of a series of “how can we do this least correctly” decisions. I develop with PHP all day. And every day, I run into the stupidity just trying to do basic things. Every day, I have to go read parts of the manual over again to remember which random ordering of arguments I should use for a given string function. Every day I deal with strange inconsistencies in the language, poor OO implementation and performance issues.
Then why use it? Lot’s of other languages out there.
That’s a rhetorical question by the way. I know why most people use PHP professionally. To make money and because a lot of web programming is now done in PHP. I’m sure lot’s of other reasons exist as well.
What amazes me most is how many people dislike PHP (according to forum post and not statistics) yet program with it everyday. I use PHP (drupal fan), but I’m by no means an expert or anything like that.
Oddly I seem to read a lot of bad things about every language. Seems no language is satisfactory. Everybody wonders what those silly language developers were thinking when they did that stupid crap.
For the record. I think the PHP developers could have come up with something a little more elegant than a backslash, but honestly those who program in PHP daily will get use to it. Like it or not. Every programmer I know worth his weight gets use to the quarks of the language they use most. Sure they bitch about it for awhile, but in the end it’s just another moot point.
That’s because that’s what we use at work, and it’s not my call. There are other reasons, based on what we do and what kind of people we can have working on stuff that make PHP preferable. Again, not my call.
I agree and realize that there are significant architectural problems with PHP (I also work with PHP every day and have been for many years), but my point was that when any PHP topic gets raised on OS news, there’s a group of posters that start ranting about things that are offtopic.
We’ve all heard the negatives many times before, it’d be great just to discuss the topic at hand, which in this case was the implementation of namespaces.
I am having trouble coming to like the ‘\’ character for namespace separator.
Why not:
$ns=>myclass->saveData();