Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:32878 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 86260 invoked by uid 1010); 20 Oct 2007 02:55:57 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 86245 invoked from network); 20 Oct 2007 02:55:57 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Oct 2007 02:55:57 -0000 Authentication-Results: pb1.pair.com smtp.mail=greg@chiaraquartet.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=greg@chiaraquartet.net; 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:45879] helo=mail.bluga.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C4/F7-23508-B3E69174 for ; Fri, 19 Oct 2007 22:55:56 -0400 Received: from mail.bluga.net (localhost.localdomain [127.0.0.1]) by mail.bluga.net (Postfix) with ESMTP id D7703C0E38B; Fri, 19 Oct 2007 19:55:52 -0700 (MST) Received: from [192.168.0.106] (CPE-76-84-13-199.neb.res.rr.com [76.84.13.199]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.bluga.net (Postfix) with ESMTP id 6886EC0E386; Fri, 19 Oct 2007 19:55:52 -0700 (MST) Message-ID: <47196F1A.80400@chiaraquartet.net> Date: Fri, 19 Oct 2007 21:59:38 -0500 User-Agent: Thunderbird 1.5.0.13 (X11/20070824) MIME-Version: 1.0 To: Chuck Hagenbuch CC: internals@lists.php.net References: <20071019224202.20245u5zry52h5c8@technest.org> In-Reply-To: <20071019224202.20245u5zry52h5c8@technest.org> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: ClamAV using ClamSMTP Subject: Re: Order of class resolution with namespaces and autoload From: greg@chiaraquartet.net (Gregory Beaver) Chuck Hagenbuch wrote: > Hi folks- > > I've hit upon something with namespaces and autoloading that I think is > a little out of order. If you take the attached 3 files, put them in the > same directory, and run demo.php, you will see this: > > Fatal error: Uncaught exception 'Exception' in > /Users/chuck/Desktop/test.php:7 > > > If you look at the code, test.php defines a Test:: namespace with a > Tester class that throws an exception, test_exception.php provides an > Exception derivative (Test::Exception) for that namespace, and demo.php > includes test.php, sets up an __autoload function to load the exception > class, and triggers the exception throwing code. > > However autoload is never called and the exception is of type > ::Exception, because the class resolution order seems to look like this: > > - does the class exist (_already defined_) in the current namespace? > - does the class exist in the global (non-prefixed or "::") namespace? > - then try autoload > > What I think should happen is: > > - does the class exist in the current namespace? > - can the class be autoloaded with the current namespace? > - does the class exist in the global scope > - global autoload Hi Chuck, Are you suggesting there be namespace-specific autoload? This sounds interesting to me, but also quite complex, not sure the benefits would outweigh the difficulties for implementation. For instance, PEAR2 namespace would be different from PEAR2::Someclass, and one would need to register the same autoload handler for each possible namespace, otherwise the class resolution code would have to grep through each classname to find sub-namespaces, which is most likely an unacceptable performance hog. > Otherwise if you are re-defining a global class, which I imagine will be > quite common with Exception at least, you will have to explicitly load > your Exception classes or they'll never be used. There is another alternative: This calls autoload, as expected, and works wonderfully. It is also quite efficient, but at the expensive of intuitiveness. However, it also has the benefit of explicitly declaring external classes at the top of the file, which is a big plus for maintainability when others are looking at your code. For me, the fact that this coding practice is both more efficient than the alternative you suggest and more maintainable makes the choice quite appealing, even if it isn't immediately intuitive. Greg