Hi Sara,

Sara Golemon wrote:
Forcing whitespace here isn't an option. It is completely inconsistent with the rest of PHP.

It's also a conversation that was had before PHP5.0 was ever released (when namespaces were first being attempted).

Anyone remember the term "syntactic sugar"?

You may be suprised by me saying this, but in a sense, yes, all of this
is just syntactic sugar. When you type "namespace ns{ class class1{} }",
all I'm doing is registering the class as "ns:class1". The user can
easily just name their class ns_class1 and the same effect can be achieved.

If you use full class names all the time, then I agree, there is no
difference between ns:class1 and ns_class1. Even when you have two
identically named classes in two different namespaces and you import
them (with different aliases, of course) this still can be considered
"syntactic sugar", as the writers of both classes were wise enough to
namespace them in the first place (they are wise enough to simply prefix
them too!).

The difference is actually in the following (off the top of my head):

1) "Enterprise-readiness" perception - Having no "namespace support" in the language makes it look crappy to people who want to use the language, especially for people who come from the Java world (please, no flame wars, I am NOT comparing PHP to Java, I am merely mentioning this perception).

2) Encouraging users to prefix their own classes - When you start writing in Java, you immediately notice that all classes, even the language-default ones, are inside a namespace. The convention of a reverse-domain name is even mentioned when you learn the language. This encourages users to namespace their own classes, as it makes their code look more organized. The language's Date class is inside java.util. If you wanted to write your own Date class, you will naturally place it inside your own package. Even if you put it under no package, there still would be no conflict! This is what I see happening for PHP: all core classes would go under a php namespace, so we'd have php:Date and PEAR:Date, for example.

3) Aliasing - Right now you're stuck with whatever name the class has. Consider this scenario: I am building a page and I am using HTML_QuickForm. Inside the form I want to have multiple multi-select boxes. My code would look like this:

$a = new HTML_QuickForm_advmultiselect(...);
$b = new HTML_QuickForm_advmultiselect(...);

This is a lot of typing. The following looks much cleaner:

import class HTML:QuickForm:advmulitselect as multiselect;
$a = new multiselect(...);
$b = new multiselect(...);


Simply put, namespaces makes code look cleaner, even though internally the same prefixing is being used to accomplish this. Since the prefixing is done by the engine itself, and we have aliasing, I wouldn't call this "sugar", I'd prefer to call it syntactic "complex carbs" ;-).


Regards,

Jessie

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to