Re: Re: PEAR2 Coding standards, Autoloading and Namespaces
| From: | Jeff Moore | Date: | Sat, 05 Apr 2008 16:53:57 +0000 |
| Subject: | Re: Re: PEAR2 Coding standards, Autoloading and Namespaces | ||
| References: | 1 2 3 4 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-49636@lists.php.net to get a copy of this message | ||
On Apr 4, 2008, at 5:28 AM, Greg Beaver wrote:
Seriously, though, the logic it takes to think in terms of packages and the classes within a package do not apply here because java distributes packages as a language feature, and enforces class naming. This requires a level of WTF that is unnecessary for PEAR. Packages are an abstract entity that are used only when downloading. The class names are what will be used on a daily basis, and I strongly encourage us to think in those terms. Which are you more likely to find natural to use, PEAR2::HTTP::Request, or PEAR2::HTTP_Request::Request? If I saw the latter classname, I would scratch my head at the redundancy, and expect the class to be in either "PEAR2/HTTP_Request/Request.php" or "PEAR2/HTTP/Request/Request.php". Both of these are confusing at best, and obfuscate the actual location at worst.So mapping package names to namespaces is a non-goal? I'm not sure the advantages of one way or the other are clear-cut. Consider two ways to organize a hypothetical package, Foo that contains two classes, Foo and Bar: EXAMPLE 1: Namesake class Foo outside the Foo namespace... PEAR2/Foo.php: namespace PEAR2; class Foo { function hello() { echo "Hello!\n"; } } PEAR2/Foo/Bar.php: namespace PEAR2::Foo; class Bar { function baz() { $obj = new PEAR2::Foo(); $obj->hello(); } } index.php: // Setup autoloader $obj = new PEAR2::Foo(); $obj->hello(); $obj = new PEAR2::Foo::Bar(); $obj->baz(); Pro: * External code can use a short and natural reference [PEAR2::Foo] to refer to the Foo class. Con: * Foo.php is not in the Foo directory (confusing, bad for version control) * The Bar class must refer to Foo with the fully qualified name, new PEAR2::Foo(). * The Foo class must use the fully qualified name to reference classes inside the PEAR2::Foo namespace (not illustrated here). * The relationship between Foo and Foo::Bar is not clear to automated tools (such as phpDocumentor). EXAMPLE 2: Namesake class Foo inside the Foo namespace... PEAR2/Foo/Foo.php: namespace PEAR2::Foo; class Foo { function hello() { echo "Hello!\n"; } } PEAR2/Foo/Bar.php: namespace PEAR2::Foo; class Bar { function baz() { $obj = new Foo(); $obj->hello(); } } index.php: // Setup autoloader $obj = new PEAR2::Foo::Foo(); $obj->hello(); $obj = new PEAR2::Foo::Bar(); $obj->baz(); Pro: * Code within the namespace can access the Foo class without fully specifying the class name, new Foo(). * Foo can access other classes in the same namespace without fully specifying the class name (not illustrated here). * All the files in the Foo namespace are in the same directory. * The relationship between the two classes is structurally clear for automated tools. Cons: * Exactly one class in the namespace, the namesake class, must be referenced outside of the package by the distasteful name, PEAR2::Foo::Foo. Jeff