- HTMList.com, A Web Development Blog by Synapse Studios - http://www.htmlist.com -

Is PHP 5.3 Terminal? Well, It’s Getting One For Namespaces

Posted By Edgar Hassler On October 27, 2008 @ 11:55 pm In Articles,Development | 5 Comments

A language lives and dies by how easy it is for a person to express something within that language. But the ease in expression is much like the type I and type II error rate [1] in a statistical test: as you adjust one to be nearer to where you want it to be, the other gets farther away. So the architects of a language have to choose between adding more keywords and constructs or having longer, but simpler sequences of existing ones.

PHP is a very easy language to learn and use because it has a syntax that is a simplified sampling from C++/Java and Perl. People who use these languages pick up PHP fast, and people who haven’t used any languages often learn PHP with as much ease as they would learn Perl.

A bit about namespaces

Most languages have namespaces [2] in one form or another. A namespace is a lot like a class [3]: it is a named entity that houses functions and classes, whereas a class is a named entity that houses properties and methods. Namespaces are found, in one form or another, in most popular languages. In C++ there are explicit namespaces and the namespace keyword. In Java, since you are allowed to have classes nested within classes, classes act as namespaces. Namespaces make it easier to organize and call your code. If you have a bunch of classes that work together to do something you might name them in a similar way. For example, we here at the Studios [4] sometimes use the following classes:

Canopy_Xml_Document
Canopy_Xml_Document_CDataSection
Canopy_Xml_Document_NodeList
Canopy_Xml_IWritable
Canopy_Xml_Exception

With namespaces, we could define a namespace Canopy, add to it a namespace Xml, and then add classes Document, Exception and interface IWritable. We could also add a Document namespace and add classes CDataSection and NodeList. Then we could just tell PHP that we want to refer to things in a certain namespace. We would simply write:

use Canopy::Xml;
$D = new Document(); //$D is equivalent to a Canopy_Xml_Document

In the beta builds of PHP 5.3, the terminal “::” called the scope resolution operator [5] (SRO) is used to denote namespaces. So, in the same way that one would write Class::staticMethod() one could write NamespaceA::NamespaceB::functionInNamespace(). This is the same operator used in C++, and it is intuitive since it refines a name to a scope where that name belongs.

PHPs SRO problem

Recently, PHP has had to abandon using the SRO, forego the syntax of other languages and has had to make up their own way of denoting a namespace. One reason for this is that bare words in PHP can have multiple meanings. For example, in PHP you can do:

const('A','constant');
function A() { return 'function'; }
class A(){ public _toString(){ return 'class'; } }

such that

echo A; // constant
echo A(); // function
echo new A(); // class

all do what you’d expect. But in namespaces, there’s some ambiguity introduced. Consider rather A::B::C() is namespace A, class B, static method C, or namespace A::B, function C. In C++ you’re not allowed to have a symbol like A, B, or C represent multiple types of things, so using the SRO does not arrive at ambiguity there. (Instead, you get an error.) But since PHP does allow for that type of thing, the internals team had to choose between:

  1. Not allowing namespaces to have functions, but rather only classes, thus raising the ire of the community.
  2. Using some terrible terrible other operator to indicate namespaces, thus raising the ire of the community. (Quoth internals team member: <@CelloG> % is also possible but holy god almighty it is fugly
  3. Put off namespaces until (or if) something decent can be figured out, thus raising the ire of the community.

The team decided on 2, going with the \ character.

What’s that mean for the language?

The discussion [6] was concerned with preventing unanticipated behavior from just making a rule to disambiguate rather a function or static method is called. I feel that throwing an error (or warning and choosing one) whenever that situation arises would be a perfectly acceptable solution. I would also have been happy to see them further follow Java’s lead and allow classes to be nested. Then there is no ambiguity between a static method and a function because they are the same. This would probably require much more work and you would have to allow class definitions across multiple files, but I feel this is a better option than ‘\’. To that end I would have enjoyed option 1 over option 2.

The most cited complaints about PHP is how there is little consistency in function names and in argument order. This new ‘\’ operator adds to the “weird-factor” which makes it more difficult for people to read and use the language. It further marginalizes PHP, adding to a series of minor but obnoxious issues proponents are embarrassed by.

The language is adrift with little direction, trying to address the wants of its developers, and adding a terminal is payment for a lack of planning and a general stubbornness of the internals list to consider namespaces back when the 5 changes were initially being discussed. On one hand I feel like this has ominous portents for PHP, but on the other, I’m really happy to have namespaces. For our domain, it still doesn’t make sense to jump ship to Groovy or Python, and it would take a lot of new terminals to push us over the edge. And while it’d be nice to see PHP not give more ammo to detractors, however menial, the discussion log makes it clear what issues they were struggling with.

Frederik Holmström points out [7] another flaw with the backslash, as well, but many commenters note that the internals team knew there was no perfect solution here.

  • email [8]

Article printed from HTMList.com, A Web Development Blog by Synapse Studios: http://www.htmlist.com

URL to article: http://www.htmlist.com/development/php-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building/

URLs in this post:

[1] type I and type II error rate: http://en.wikipedia.org/wiki/Type_I_and_type_II_errors

[2] namespaces: http://en.wikipedia.org/wiki/Namespace

[3] class: http://en.wikipedia.org/wiki/Class_(computer_science)

[4] Studios: http://www.synapsestudios.com/

[5] scope resolution operator: http://en.wikipedia.org/wiki/Scope_resolution_operator

[6] discussion: http://wiki.php.net/_media/rfc/php.ns.txt?id=rfc%3Anamespaceseparator&cache=cache

[7] points out: http://loveandtheft.org/2008/10/26/set-sail-for-fail-php-namespaces/

[8] Image: https://mail.google.com/mail/?view=cm&fs=1&to&su=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces&body=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&ui=2&tf=1&shva=1

[9] Image: http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&t=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces

[10] Image: http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&t=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces

[11] Image: http://twitter.com/intent/tweet?text=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces%20-%20http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F%20 SHARETAG

[12] Image: http://www.linkedin.com/shareArticle?mini=true&url=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&title=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces&source=HTMList.com%2C+A+Web+Development+Blog+by+Synapse+Studios+A+Web+Development+Blog+by+Synapse+Studios&summary=A%20language%20lives%20and%20dies%20by%20how%20easy%20it%20is%20for%20a%20person%20to%20express%20something%20within%20that%20language.%20%20But%20the%20ease%20in%20expression%20is%20much%20like%20the%20type%20I%20and%20type%20II%20error%20rate%20in%20a%20statistical%20test%3A%20%20as%20you%20adjust%20one%20to%20be%20nearer%20to%20where%20you%20want%20it

[13] Image: http://delicious.com/post?url=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&title=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces&notes=A%20language%20lives%20and%20dies%20by%20how%20easy%20it%20is%20for%20a%20person%20to%20express%20something%20within%20that%20language.%20%20But%20the%20ease%20in%20expression%20is%20much%20like%20the%20type%20I%20and%20type%20II%20error%20rate%20in%20a%20statistical%20test%3A%20%20as%20you%20adjust%20one%20to%20be%20nearer%20to%20where%20you%20want%20it

[14] Image: http://digg.com/submit?phase=2&url=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&title=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces&bodytext=A%20language%20lives%20and%20dies%20by%20how%20easy%20it%20is%20for%20a%20person%20to%20express%20something%20within%20that%20language.%20%20But%20the%20ease%20in%20expression%20is%20much%20like%20the%20type%20I%20and%20type%20II%20error%20rate%20in%20a%20statistical%20test%3A%20%20as%20you%20adjust%20one%20to%20be%20nearer%20to%20where%20you%20want%20it

[15] Image: http://reddit.com/submit?url=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&title=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces

[16] Image: http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&title=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces

[17] Image: http://www.google.com/bookmarks/mark?op=edit&bkmk=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&title=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces&annotation=A%20language%20lives%20and%20dies%20by%20how%20easy%20it%20is%20for%20a%20person%20to%20express%20something%20within%20that%20language.%20%20But%20the%20ease%20in%20expression%20is%20much%20like%20the%20type%20I%20and%20type%20II%20error%20rate%20in%20a%20statistical%20test%3A%20%20as%20you%20adjust%20one%20to%20be%20nearer%20to%20where%20you%20want%20it

[18] Image: http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&t=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces

[19] Image: http://reporter.es.msn.com/?fn=contribute&Title=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces&URL=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&cat_id=6&tag_id=31&Remark=A%20language%20lives%20and%20dies%20by%20how%20easy%20it%20is%20for%20a%20person%20to%20express%20something%20within%20that%20language.%20%20But%20the%20ease%20in%20expression%20is%20much%20like%20the%20type%20I%20and%20type%20II%20error%20rate%20in%20a%20statistical%20test%3A%20%20as%20you%20adjust%20one%20to%20be%20nearer%20to%20where%20you%20want%20it

[20] Image: http://www.blinklist.com/index.php?Action=Blink/addblink.php&Url=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&Title=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces

[21] Image: http://sphinn.com/index.php?c=post&m=submit&link=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F

[22] Image: http://posterous.com/share?linkto=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&title=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces&selection=A%20language%20lives%20and%20dies%20by%20how%20easy%20it%20is%20for%20a%20person%20to%20express%20something%20within%20that%20language.%20%20But%20the%20ease%20in%20expression%20is%20much%20like%20the%20type%20I%20and%20type%20II%20error%20rate%20in%20a%20statistical%20test%3A%20%20as%20you%20adjust%20one%20to%20be%20nearer%20to%20where%20you%20want%20it

[23] Image: http://www.tumblr.com/share?v=3&u=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&t=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces&s=A%20language%20lives%20and%20dies%20by%20how%20easy%20it%20is%20for%20a%20person%20to%20express%20something%20within%20that%20language.%20%20But%20the%20ease%20in%20expression%20is%20much%20like%20the%20type%20I%20and%20type%20II%20error%20rate%20in%20a%20statistical%20test%3A%20%20as%20you%20adjust%20one%20to%20be%20nearer%20to%20where%20you%20want%20it

[24] Image: http://www.google.com/reader/link?url=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&title=Is%20PHP%205.3%20Terminal%3F%20%20Well%2C%20It%27s%20Getting%20One%20For%20Namespaces&srcURL=http%3A%2F%2Fwww.htmlist.com%2Fdevelopment%2Fphp-5-3-namespaces-backslashes-terminals-and-you-the-art-of-language-building%2F&srcTitle=HTMList.com%2C+A+Web+Development+Blog+by+Synapse+Studios+A+Web+Development+Blog+by+Synapse+Studios

[25] Tweet: https://twitter.com/share

Copyright © 2008 HTMList. All rights reserved.