Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:33866 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 20828 invoked by uid 1010); 8 Dec 2007 18:38:13 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 20812 invoked from network); 8 Dec 2007 18:38:12 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Dec 2007 18:38:12 -0000 Authentication-Results: pb1.pair.com header.from=greg@chiaraquartet.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=greg@chiaraquartet.net; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain chiaraquartet.net from 38.99.98.18 cause and error) X-PHP-List-Original-Sender: greg@chiaraquartet.net X-Host-Fingerprint: 38.99.98.18 beast.bluga.net Linux 2.6 Received: from [38.99.98.18] ([38.99.98.18:51156] helo=mail.bluga.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D1/47-51488-194EA574 for ; Sat, 08 Dec 2007 13:38:11 -0500 Received: from mail.bluga.net (localhost.localdomain [127.0.0.1]) by mail.bluga.net (Postfix) with ESMTP id 0394DC0EA79; Sat, 8 Dec 2007 11:38:05 -0700 (MST) Received: from [192.168.0.106] (CPE-76-84-15-179.neb.res.rr.com [76.84.15.179]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.bluga.net (Postfix) with ESMTP id 85FA8C0EA76; Sat, 8 Dec 2007 11:38:04 -0700 (MST) Message-ID: <475AE494.1070609@chiaraquartet.net> Date: Sat, 08 Dec 2007 12:38:12 -0600 User-Agent: Thunderbird 2.0.0.6 (X11/20071022) MIME-Version: 1.0 To: Matthias Pigulla CC: PHP Developers Mailing List References: <4758A390.4040402@chiaraquartet.net> <00A2E2156BEE8446A81C8881AE117F199A06B9@companyweb> <4759848D.5050007@chiaraquartet.net> <00A2E2156BEE8446A81C8881AE117F199A06C2@companyweb> In-Reply-To: <00A2E2156BEE8446A81C8881AE117F199A06C2@companyweb> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: ClamAV using ClamSMTP Subject: Re: AW: [PHP-DEV] A rebuttal to Re: RFC: Dropping Namespace From: greg@chiaraquartet.net (Gregory Beaver) Matthias Pigulla wrote: >> Von: Gregory Beaver [mailto:greg@chiaraquartet.net] > >> Exactly - which is why you should never put classes, functions or >> constants in the __php__ namespace. The convention I am proposing >> is to only use __php__ for code that *uses* re-usable components, >> not *declares* them. > > Let alone __php__. If you just put all of your code into namespace > Mylib, you're not safe because according to the name resolution > rules, internal classes come after imported ones but before trying to > find classes in the current namespace. Right, but as long as you know this, it is not so bad for two reasons, see below. Even if you *don't* know this, the chance of a name collision is extremely unlikely - the most common collision will be Exception. >> 1) library code is always explicitly "use"d 2) name conflicts are >> impossible > > 1) is the crucial one because that puts your classes ahead of the > internal ones in the resolution list. That is not only "library code" > you explicitly use, but also all code from "your" own namespace. > Having to explicitly enumerate all classes you use in your own > namespace in every file may be tedious. > > So just to get that straight: Having a namespace statement and no > "use" (because all you use is from your library) is a discouraged > practise? To be clear - this *only* affects users who are relying on autoload. In other words, this code works the same way 100% of the time regardless of load order: file1.php: file2.php: So, if your code explicitly loads external files using include/require, you need not worry about this issue. Library authors who are in fact relying upon autoload do need to explicitly use the classes they import as unqualified names, but this is far less onerous than it seems on initial thought. Let's say you're using 10 classes from your own project. The first instinct is that you then have to have 10 use lines - what a pain! However, this is not necessary. Here's a realistic sample from PEAR2_Pyrus's package.xml validator class, PEAR2::Pyrus::PackageFile::v2::Validator. With the above use statements, I can access these classes: PEAR2::Pyrus::PackageFile::v2 PEAR2::Pyrus::Validate PEAR2::Pyrus::PackageFile::Exception PEAR2::Pyrus::ChannelRegistry PEAR2::Pyrus::Installer::Role PEAR2::Pyrus::Config PEAR2::Pyrus::Config::Exception PEAR2::Pyrus::Task PEAR2::Pyrus::Log PEAR2::Pyrus::Package::Tar with these shortcuts: pf::v2 me::Validate _ex me::ChannelRegistry me::Installer::Role me::Config me::Config::Exception me::Task me::Log me::Package::Tar Note that it would be possible to shorten the longer names like me::Config::Exception, but these classnames are only used once in the file. The most commonly used classname is PEAR2::Pyrus::PackageFile::Exception, and so I would choose that as the shortest name. When one examines these things from a practical standpoint, having the 4 extra characters "me::" definitely increases maintainability, as it makes it obvious that this class is from our library, and not an internal class. I do think it would be worth putting future internal classes into the PHP namespace, and it would be a good idea to reserve that namespace now. The reservation can always be dropped later if necessary. Greg