Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:38472 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 92255 invoked from network); 20 Jun 2008 15:49:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Jun 2008 15:49:27 -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:47306] helo=mail.bluga.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 85/F1-16112-581DB584 for ; Fri, 20 Jun 2008 11:49:26 -0400 Received: from mail.bluga.net (localhost.localdomain [127.0.0.1]) by mail.bluga.net (Postfix) with ESMTP id 8BA02C131F7; Fri, 20 Jun 2008 08:49:28 -0700 (MST) Received: from [192.168.0.106] (CPE-76-84-4-101.neb.res.rr.com [76.84.4.101]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.bluga.net (Postfix) with ESMTP id DA451C131DE; Fri, 20 Jun 2008 08:49:27 -0700 (MST) Message-ID: <485BD1C0.8040302@chiaraquartet.net> Date: Fri, 20 Jun 2008 10:50:24 -0500 User-Agent: Thunderbird 2.0.0.14 (X11/20080505) MIME-Version: 1.0 To: Andi Gutmans , Stanislav Malyshev , Dmitry Stogov , internals Mailing List X-Enigmail-Version: 0.95.0 Content-Type: multipart/mixed; boundary="------------010408090402020909050706" X-Virus-Scanned: ClamAV using ClamSMTP Subject: simple solution to another namespace conundrum? From: greg@chiaraquartet.net (Gregory Beaver) --------------010408090402020909050706 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi, You probably have seen Derick's blog post http://www.derickrethans.nl/namespaces_in_php.php It occurred to me today that there might be a simple, elegant solution to this problem. First, let's imagine someone writes this code: Now, in PHP 5.4, we introduce an internal class "Closure" and the user's code suddenly breaks on the "use" statement because we check for an internal name conflict. Here's the kicker: why do we need to worry about a name conflict? This user's code was designed to work exclusively with Fancy::Closure, and doesn't care about any present or future internal classes that conflict! Also, because this is a compile-time alias that only affects the current script file, if we were to allow the above user's code to essentially override the internal Closure class, there is no possible harm because 1) the user explicitly imported Fancy::Closure 2) the user therefore never intends to use the internal ::Closure in this script In fact, the same thing is true for existing classnames. Why should we care if a user does this? The user is obviously intentionally creating a "DateTime" class, and doesn't care about the internal classname in this script. The attached patch against PHP_5_3 would fix the issue by eliminating the check for conflict with CG(class_table) in the global namespace for internal classes. It however preserves this check for userspace classes so that (for instance) php-src/tests/Zend/ns_030.phpt does not fail. The basic idea is that we do have control over the userspace classes we include, but have no control over the internal classes. A new test would also be needed: 066.php.inc: --TEST-- 066: Name ambiguity (import name & internal class name) --FILE-- type == ZEND_USER_CLASS) { + char *tmp = zend_str_tolower_dup(Z_STRVAL_P(ns), Z_STRLEN_P(ns)); + + if (Z_STRLEN_P(ns) != Z_STRLEN_P(name) || + memcmp(tmp, lcname, Z_STRLEN_P(ns))) { + zend_error(E_COMPILE_ERROR, "Cannot use %s as %s because the name is already in use", Z_STRVAL_P(ns), Z_STRVAL_P(name)); + } + efree(tmp); } - efree(tmp); } if (zend_hash_add(CG(current_import), lcname, Z_STRLEN_P(name)+1, &ns, sizeof(zval*), NULL) != SUCCESS) { --------------010408090402020909050706--