Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:32877 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 83863 invoked by uid 1010); 20 Oct 2007 02:42:07 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 83848 invoked from network); 20 Oct 2007 02:42:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Oct 2007 02:42:07 -0000 Authentication-Results: pb1.pair.com smtp.mail=chuck@horde.org; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=chuck@horde.org; sender-id=pass Received-SPF: pass (pb1.pair.com: domain horde.org designates 66.92.78.250 as permitted sender) X-PHP-List-Original-Sender: chuck@horde.org X-Host-Fingerprint: 66.92.78.250 dsl092-078-250.bos1.dsl.speakeasy.net Received: from [66.92.78.250] ([66.92.78.250:44719] helo=technest.org) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 75/A7-23508-DFA69174 for ; Fri, 19 Oct 2007 22:42:07 -0400 Received: by technest.org (Postfix, from userid 33) id D79CE7F00D; Fri, 19 Oct 2007 22:42:02 -0400 (EDT) Received: from tatiana (tatiana [192.168.1.2]) by technest.org (Horde Framework) with HTTP; Fri, 19 Oct 2007 22:42:02 -0400 Message-ID: <20071019224202.20245u5zry52h5c8@technest.org> Date: Fri, 19 Oct 2007 22:42:02 -0400 To: internals@lists.php.net MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=_5ssu9d003hgk" Content-Transfer-Encoding: 7bit User-Agent: Internet Messaging Program (IMP) H3 (4.2-cvs) Subject: Order of class resolution with namespaces and autoload From: chuck@horde.org (Chuck Hagenbuch) --=_5ssu9d003hgk Content-Type: text/plain; charset=ISO-8859-1; DelSp="Yes"; format="flowed" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi folks- I've hit upon something with namespaces and autoloading that I think =20 is a little out of order. If you take the attached 3 files, put them =20 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 =20 Tester class that throws an exception, test_exception.php provides an =20 Exception derivative (Test::Exception) for that namespace, and =20 demo.php includes test.php, sets up an __autoload function to load the =20 exception class, and triggers the exception throwing code. However autoload is never called and the exception is of type =20 ::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 Otherwise if you are re-defining a global class, which I imagine will =20 be quite common with Exception at least, you will have to explicitly =20 load your Exception classes or they'll never be used. The other alternative would be to say that if you want to use a global =20 class inside a namespace you MUST prefix it with ::. I think this is =20 reasonable since if you have a namespaced file it's already not going =20 to work with anything before PHP 5.3, and that would remove the two =20 potential autoload checks. Thanks, -chuck --=_5ssu9d003hgk Content-Type: text/plain; charset=UTF-8; name="demo.php" Content-Disposition: attachment; filename="demo.php" Content-Transfer-Encoding: 7bit fail(); --=_5ssu9d003hgk Content-Type: text/plain; charset=UTF-8; name="test.php" Content-Disposition: attachment; filename="test.php" Content-Transfer-Encoding: 7bit