Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:38499 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24544 invoked from network); 21 Jun 2008 19:29:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Jun 2008 19:29:31 -0000 X-Host-Fingerprint: 74.161.249.64 adsl-161-249-64.mia.bellsouth.net Received: from [74.161.249.64] ([74.161.249.64:29910] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 47/43-26505-9965D584 for ; Sat, 21 Jun 2008 15:29:30 -0400 To: internals@lists.php.net,Gregory Beaver Message-ID: <485D5696.4070408@gmail.com> Date: Sat, 21 Jun 2008 15:29:26 -0400 User-Agent: Thunderbird 2.0.0.12 (X11/20080226) MIME-Version: 1.0 CC: Andi Gutmans , Stanislav Malyshev , Dmitry Stogov References: <485BD1C0.8040302@chiaraquartet.net> In-Reply-To: <485BD1C0.8040302@chiaraquartet.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 74.161.249.64 Subject: Re: simple solution to another namespace conundrum? From: jrhernandez05@gmail.com (Jessie Hernandez) Hi Greg, How is this different from my original proposal (http://news.php.net/php.internals/34097, http://news.php.net/php.internals/34097)? -- Jessie Hernandez Zend Certified Engineer (http://zend.com/zce.php?c=ZEND006359&r=222727282) Gregory Beaver wrote: > 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: > > include '/path/to/library/Closure.php'; > use Fancy::Closure; > > $a = new Closure('for ($a = 1;$a<10;$a++) docrap();'); > ?> > > 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? > > include '/path/to/my/date/lib.php'; > use My::DateTime; > > $a = new DateTime('2006/04/05'); > ?> > > 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: > namespace A; > class B > { > function __construct(){echo __CLASS__;} > } > ?> > > --TEST-- > 066: Name ambiguity (import name & internal class name) > --FILE-- > include __DIR__ . '/066.php.inc'; > use A::B as stdClass; > > new stdClass(); > --EXPECT-- > A::B > > Thanks, > Greg >