Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62884 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 1168 invoked from network); 7 Sep 2012 00:48:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Sep 2012 00:48:32 -0000 Authentication-Results: pb1.pair.com smtp.mail=smalyshev@sugarcrm.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=smalyshev@sugarcrm.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain sugarcrm.com designates 67.192.241.163 as permitted sender) X-PHP-List-Original-Sender: smalyshev@sugarcrm.com X-Host-Fingerprint: 67.192.241.163 smtp163.dfw.emailsrvr.com Linux 2.6 Received: from [67.192.241.163] ([67.192.241.163:55346] helo=smtp163.dfw.emailsrvr.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2F/61-03079-F5449405 for ; Thu, 06 Sep 2012 20:48:31 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp26.relay.dfw1a.emailsrvr.com (SMTP Server) with ESMTP id 8D96D800B6; Thu, 6 Sep 2012 20:48:28 -0400 (EDT) X-Virus-Scanned: OK Received: by smtp26.relay.dfw1a.emailsrvr.com (Authenticated sender: smalyshev-AT-sugarcrm.com) with ESMTPSA id 29B498012B; Thu, 6 Sep 2012 20:48:28 -0400 (EDT) Message-ID: <5049445B.603@sugarcrm.com> Date: Thu, 06 Sep 2012 17:48:27 -0700 Organization: SugarCRM User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:15.0) Gecko/20120824 Thunderbird/15.0 MIME-Version: 1.0 To: Mark CC: "internals@lists.php.net" References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Why are the PHP namespaces different compared to C++? From: smalyshev@sugarcrm.com (Stas Malyshev) Hi! > In C++ when you type: > using std; > > Then you can use all methods/classes/whatever is defined in std > without typing std. > so: std::cout becomes just cout. PHP namespaces do not work like that. In PHP, namespace is a permanent part of the class/function name. All namespace translations are done in compile-time (which is not the same as compile-time in C++, btw) and are simple alias translations. PHP explicitly avoids mass imports, because this would lead to context pollution and interoperability problems - i.e., if you have some function named "foo" in two namespaces and you import both, you may have problems. And since imported namespaces usually mean somebody else's code, you may not have control or good information about changes, making importing code fragile. Since unlike C++ PHP does not have compiler that can resolve all the mess before it is run, PHP has to be more conservative in order to avoid problems. For this reason, PHP's "use" is nothing more than aliasing operator. It does not put any other names into global space but the names specifically mentioned by it. So, if you have to do std::cout, it'd still be std\cout, but if you want to do some\long\import\namespace\cout, you can alias some\long\import\namespace to mystd, and do mystd\cout. So you never have to do more than one \, but you still have to do one, to avoid polluting global space. > But i'm wondering why the "use Some\Long\Namespace" doesn't work like > the C++ one. Since i would have guessed that adding that use will give > me access to all the methods/classes/whatever that live within that > namespace _without_ having to prefix it with the last part of the > namespace. This is exactly what we were trying to avoid - for the reasons described above. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227