Tag: syntax

PHP Namespaces

Posted by – October 26, 2008

I have been seeing alot of complaining lately regarding PHP namespaces, and I thought I would chime in with my (often opposing) views. First off, let me explain the issue.

The new version of PHP will have a new feature, called namespaces. (I wrote about it in my post”PHP 5.3 Feature Preview“. This is a great featured, and one that the community as a whole is excited about. So what is the problem, then?

Well initially PHP was going to use the standard “::” syntax to invoke the namespace. For example:


namespace Foo;
   function bar() {
      echo "Namespace Foo";
   }
   Foo::bar();

However, this became a problem for the parsing engine, as it is the same way to call a static function.

class Foo{
   static function bar(){
      echo "Class Foo";
   }
}
Foo::bar();

So instead, PHP changed the syntax to a blackslash. So now, in the first example, you have ‘Foo\bar();’ while in the second, you still have ‘Foo::bar();’ Seems reasonable to me (and the PHP core members) but not to some.

For example, Ninh complains on his blog that if you put the invocation in double quotes, it will interpret things like “\t” as a tab, and that you have to use 2 backslashes. I really don’t see this being a problem. First off, why on earth would you use double quotes? There are no variables or single quotes being used in the string, so one should be using single quotes anyways. That is just good programming practice.

Even if you do use double quotes, there is a tried and true, standard solution to escape the backslash character. It is so obiquitous, that Ninh didn’t have to learn how to use it, he already knew about it, yet is still complaining. His problem with this method? “It looks like crap”. I’m sorry, I though we were using logic to code web applications, not painting a picture. And even at that, why does “::” look awesome, but “\\” look like crap? I don’t get it.

My favorite quote if from the very end of his post. He claims “Last time I checked, the world wasn’t filled with scrawny developers that would come crying to their mommies after getting their first facepalm of ‘AmbiguousInvocationError’.” And yet he is crying to his mommy (or rather, the blogosphere) because if he uses double quotes (which he doesn’t have to) then he has to escape the backslash character (a standard practice) and that’s “ugly”.

So what are your thoughts? Maybe I’m crazy, and this is a huge deal. I just don’t see it.

multiple line strings in C Sharp

Posted by – July 19, 2007

Today I found something that made my day. I have always hated that you can’t have strings span multiple lines of code in ASP.NET. For VB.NET this makes since, because there is no semi-colon at the end of statements. To get strings to span multiple lines in VB.NET, you can add an ampersand + space + underscore to the end of each line, like so:


dim sample as string = "notice how " & _
"the string is on multiple lines!" & _
"just make sure you put double quotes on the" & _
"beginning and end of each line"

This works for VB.NET, but not so for C#.NET. I have tried changing the ampersand to a plus sign (c sharp’s character to concatenate strings) but this didn’t work. I had assumed it was impossible, until I came across a solution today. Simply put an ‘at’ sign (@) before the string!


string sample = @"notice how
the string is on multiple lines!";

As you can see, in c-sharp.net you don’t need to start and stop the string like you do in vb.net. The only thing that is slightly tricky, is if you start concatenating strings. Then you need to add the @ symbol to every section of string that will be on multiple lines. For example:


string sample = @"this string
is on multiple lines" + someVariable + @" notice
how i had to use an 'at' symbol again, 
however i don't if the string section stays on one line, 
such as" + here + "... get it?";

Enjoy this ability to word-wrap you sql queries, or any other code strings which need to span multiple lines!