Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62880 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 95506 invoked from network); 7 Sep 2012 00:32:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Sep 2012 00:32:50 -0000 Authentication-Results: pb1.pair.com smtp.mail=g.b.yahav@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=g.b.yahav@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.170 as permitted sender) X-PHP-List-Original-Sender: g.b.yahav@gmail.com X-Host-Fingerprint: 209.85.220.170 mail-vc0-f170.google.com Received: from [209.85.220.170] ([209.85.220.170:33839] helo=mail-vc0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0E/20-03079-0B049405 for ; Thu, 06 Sep 2012 20:32:49 -0400 Received: by vchn11 with SMTP id n11so3506153vch.29 for ; Thu, 06 Sep 2012 17:32:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=akKlLPeO6jY0W7PEEbfgZ493u3AJaNsMXwt1DzRMl+Y=; b=jOX+mdQvtf0AfyQDvZoorDipdGK2zP5+y5Z/HFe+2KwCpqDTuy2I6Df5Xeq8cIE6Wq VuMgBwHtKjT58elqpPuDfpn3i2wKqJZfhU9hqml4Txqs5B3ip8Auz0kwVF5kB9U3/nZ6 CoFOetGhIDsdf1XoVxYzk0AoYNJyRH+d0JPRqhIX3/KiP18L2aTvIaU9YUfHLeGs4GwL mTLlIXI73nsudwJBnTDpSOcwwEDSylY3zRZp2I0AARwhgc9Qk7LetOM3LIfmJ3rHoWiq s/uLX9Ylu7WVlHuvOy+IiYtI3YNZyyGWFMIl0bIHIIw/wBRKl88rEb5TibxFrlKEah04 NSZg== Received: by 10.52.95.46 with SMTP id dh14mr3969972vdb.114.1346977966226; Thu, 06 Sep 2012 17:32:46 -0700 (PDT) MIME-Version: 1.0 Received: by 10.52.0.69 with HTTP; Thu, 6 Sep 2012 17:32:25 -0700 (PDT) In-Reply-To: References: Date: Fri, 7 Sep 2012 03:32:25 +0300 Message-ID: To: Levi Morrison Cc: Sherif Ramadan , Mark , internals@lists.php.net Content-Type: multipart/alternative; boundary=20cf3071cc68327bc004c911bf65 Subject: Re: [PHP-DEV] Why are the PHP namespaces different compared to C++? From: g.b.yahav@gmail.com (Yahav Gindi Bar) --20cf3071cc68327bc004c911bf65 Content-Type: text/plain; charset=ISO-8859-1 On Fri, Sep 7, 2012 at 3:21 AM, Levi Morrison wrote: > On Thu, Sep 6, 2012 at 6:15 PM, Sherif Ramadan > wrote: > > On Thu, Sep 6, 2012 at 5:30 PM, Mark wrote: > >> Hi, > >> > >> I was just using the PHP namespaces for the first time and noticed a > >> difference that i really didn't expect. (No, i won't start complaining > >> about the slash based namespace). > >> > >> 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. > >> > >> In PHP that's a bit different. Lets take this as an example: > >> ================================= > >> namespacetst.php > >> > >> >> namespace Some\Long\Namespace; > >> > >> Class SomeClass > >> { > >> } > >> > >> > >> Now the one using that "SomeClass" has to do something like this: > >> index.php > >> use Some\Long\Namespace; > >> > >> $oClass = new Namespace\SomeClass(); > >> ================================= > >> > >> Yes, you can also do: > >> > >> index.php > >> use Some\Long\Namespace\SomeClass; > >> $oClass = new SomeClass(); > >> > >> 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. > >> > >> I hope someone can shed some light over this. > >> > >> Regards, > >> Mark > >> > >> -- > >> PHP Internals - PHP Runtime Development Mailing List > >> To unsubscribe, visit: http://www.php.net/unsub.php > >> > > > > > > Yes, PHP namespaces are completely different from what you'd be used > > to in C++. In all honesty namespaces were never well designed in PHP > > and were implemented in a haphazard way, which is why I generally > > don't bother using them. > > > > To clarify, importing namespaces in PHP isn't like importing > > namespaces in C++ at all, really. You are merely aliasing namespaces > > in PHP when you use the "use" keyword. Meaning that what's actually > > happening is PHP expects you to alias one namespace to another (and > > thus you can never import one namespace directly into the existing > > namespace since this creates a name conflict). It's messy... > > > > NameSpacedFile.php > > namespace My\Name\Space; > > class MyClass { } > > > > > > Index.php > > use My\Name\Space; // This doesn't actually import anything > > // What happened here is we created an alias > > // It's the same thing as saying "use My\Name\Space as \Space > > // So now My\Name\Space is aliased to \Space > > > > $obj = new \Space\MyClass; // great > > $obj = new MyClass; // this won't do what you want > > > > > > Here's the full example: > > > > http://viperpad.com/qcAUNM > > > > It simply doesn't work like that because PHP's namespaces are > > implemented in such a way that they don't really resemble namespaces. > > Just some fancy magic going on in the engine that allow it to mimic > > namespaces. > > While that is true, I cannot stress their importance in organizing > code. Nearly every codebase I've seen that can use namespaces maps > the namespace to the file system which makes for very easy-to-code > autoloaders. Very helpful indeed. It would be nice to see improved > namespaces, though. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > Will the current implementation of namespace designed for import all classes, functions and such from namespace? or it should be rewritten entirely in order to support that? How about adding ability to import the entire namespace? --20cf3071cc68327bc004c911bf65--