Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:32898 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 20677 invoked by uid 1010); 21 Oct 2007 02:23:40 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 20661 invoked from network); 21 Oct 2007 02:23:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Oct 2007 02:23:40 -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:58624] helo=mail.bluga.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EC/24-30596-A28BA174 for ; Sat, 20 Oct 2007 22:23:39 -0400 Received: from mail.bluga.net (localhost.localdomain [127.0.0.1]) by mail.bluga.net (Postfix) with ESMTP id 5BAE2D0F092; Sat, 20 Oct 2007 19:23:36 -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 A5839D0F0DF; Sat, 20 Oct 2007 19:23:35 -0700 (MST) Message-ID: <471AB908.6080209@chiaraquartet.net> Date: Sat, 20 Oct 2007 21:27:20 -0500 User-Agent: Thunderbird 1.5.0.13 (X11/20070824) MIME-Version: 1.0 To: Chuck Hagenbuch CC: Stanislav Malyshev , internals@lists.php.net References: <20071020193946.12273zalj5q8cngg@technest.org> In-Reply-To: <20071020193946.12273zalj5q8cngg@technest.org> X-Enigmail-Version: 0.94.2.0 Content-Type: multipart/mixed; boundary="------------060108090600030803040303" X-Virus-Scanned: ClamAV using ClamSMTP Subject: Re: [PHP-DEV] Re: Order of class resolution with namespaces andautoload From: greg@chiaraquartet.net (Gregory Beaver) --------------060108090600030803040303 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Chuck Hagenbuch wrote: > Quoting Stanislav Malyshev : > >> This is even better than requiring, and makes the intent very clear. I >> don't think it decreases intuitiveness, on the contrary - from the >> look of the code it is immediately clear which exception would be used. > > I went ahead and tried to apply this (explicit imports of classes in the > same namespace) to the library that I'm working on, and it doesn't work. > Here's the same example as before with 1.php and 2.php, but this time > we're trying to avoid ambiguity by importing Test::Exception in each file: > > 1.php: > ------ > > namespace Test; > import Test::Exception; > > throw new Exception(); > > > 2.php: > ------ > > namespace Test; > import Test::Exception; > > throw new Exception(); > > > (yes, they are identical; imagine that one defines, say, Test::Runner > and the other defines Test::Case, both of which can throw Test::Exception) > > Now I run parent.php, which looks like: > --------------------------------------- > > > function __autoload($class) { > include './test_exception.php'; > } > > try { > include '2.php'; > } catch (Exception $e) { > echo get_class($e) . "\n"; > } > > try { > include '1.php'; > } catch (Exception $e) { > echo get_class($e) . "\n"; > } > > > > And I get: > ---------- > $ php parent.php > Test::Exception > > Fatal error: Import name 'Exception' conflicts with defined class in > /Users/chuck/Desktop/php namespaces/1.php on line 4 > > This one I can't solve by removing the use of autoload, either - if I > include test_exception.php before doing anything, it just fails on the > first file that imports it: > > $ php parent.php > > Fatal error: Import name 'Exception' conflicts with defined class in > /Users/chuck/Desktop/php namespaces/2.php on line 4 Hi Chuck, Turns out you've found a bug in the fix for Bug #42859. I've attached a patch for PHP 5.3, and can do the same for PHP 6 later. Greg --------------060108090600030803040303 Content-Type: text/plain; name="fix_import_again.patch.txt" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="fix_import_again.patch.txt" Index: Zend/zend_compile.c =================================================================== RCS file: /repository/ZendEngine2/zend_compile.c,v retrieving revision 1.647.2.27.2.41.2.10 diff -u -r1.647.2.27.2.41.2.10 zend_compile.c --- Zend/zend_compile.c 17 Oct 2007 10:01:21 -0000 1.647.2.27.2.41.2.10 +++ Zend/zend_compile.c 21 Oct 2007 01:59:54 -0000 @@ -4596,7 +4596,7 @@ { char *lcname; zval *name, *ns, tmp; - zend_bool warn = 0; + zend_bool warn = 0, shorthand = 0; if (!CG(current_import)) { CG(current_import) = emalloc(sizeof(HashTable)); @@ -4611,11 +4611,12 @@ char *p; /* The form "import A::B" is eqivalent to "import A::B as B". - So we extract the last part of compound name ti use as a new_name */ + So we extract the last part of compound name to use as a new_name */ name = &tmp; p = zend_memrchr(Z_STRVAL_P(ns), ':', Z_STRLEN_P(ns)); if (p) { ZVAL_STRING(name, p+1, 1); + shorthand = 1; } else { *name = *ns; zval_copy_ctor(name); @@ -4640,7 +4641,8 @@ ns_name[Z_STRLEN_P(CG(current_namespace))] = ':'; ns_name[Z_STRLEN_P(CG(current_namespace))+1] = ':'; memcpy(ns_name+Z_STRLEN_P(CG(current_namespace))+2, lcname, Z_STRLEN_P(name)+1); - if (zend_hash_exists(CG(class_table), ns_name, Z_STRLEN_P(CG(current_namespace)) + 2 + Z_STRLEN_P(name)+1)) { + /* if our new import name is simply the shorthand, skip this check */ + if (!shorthand || !memcmp(ns_name, Z_STRVAL_P(ns), Z_STRLEN_P(ns) + 1) && zend_hash_exists(CG(class_table), ns_name, Z_STRLEN_P(CG(current_namespace)) + 2 + Z_STRLEN_P(name)+1)) { zend_error(E_COMPILE_ERROR, "Import name '%s' conflicts with defined class", Z_STRVAL_P(name)); } efree(ns_name); Index: Zend/tests/namespace_import1.inc =================================================================== RCS file: Zend/tests/namespace_import1.inc diff -N Zend/tests/namespace_import1.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ Zend/tests/namespace_import1.inc 21 Oct 2007 01:59:55 -0000 @@ -0,0 +1,3 @@ + +--EXPECT-- +caught \ No newline at end of file --------------060108090600030803040303--