Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62878 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 92284 invoked from network); 7 Sep 2012 00:15:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Sep 2012 00:15:31 -0000 Authentication-Results: pb1.pair.com header.from=theanomaly.is@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=theanomaly.is@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.170 as permitted sender) X-PHP-List-Original-Sender: theanomaly.is@gmail.com X-Host-Fingerprint: 209.85.217.170 mail-lb0-f170.google.com Received: from [209.85.217.170] ([209.85.217.170:55755] helo=mail-lb0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1D/7F-03079-1AC39405 for ; Thu, 06 Sep 2012 20:15:30 -0400 Received: by lbbgp3 with SMTP id gp3so1612751lbb.29 for ; Thu, 06 Sep 2012 17:15:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=h400OsTmCZMYGI1jjNWjSgE89fJCHefzhrP3AGW7VGI=; b=JzrCq4Jg96J4v8qWqH5Z2sjA9CFffelZs2vehT5FvSOt5x8CHxe84xKM2fzzM/cTQ0 WKhHAoYkm1qvbpZFBQ/fe9SsHjUldpNt4z1xpnHegHur7/cYQ2IrfcwR+Ka8pcILKfwv I/hpr3poCm9z0fLrqdvN8TkOsZzJuFaPviB9NW2SZQXuxQBhDCmAMC2d9lBaRESKlLU2 6W+Uaytpe1PhlYp+hPImYIpIVUtpiA/euhg1zvXQF0mtg/iAwfSM7bbrgYtaotjWwjfT zLeBCqYCARRB51r/vFrTYLzJ0vfQtgMC377q1XTKaIKHfWLhZnYN3Ru/IIhK1FElN+ua yEyQ== MIME-Version: 1.0 Received: by 10.112.10.170 with SMTP id j10mr1516977lbb.39.1346976927017; Thu, 06 Sep 2012 17:15:27 -0700 (PDT) Received: by 10.112.12.178 with HTTP; Thu, 6 Sep 2012 17:15:26 -0700 (PDT) In-Reply-To: References: Date: Thu, 6 Sep 2012 20:15:26 -0400 Message-ID: To: Mark Cc: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Why are the PHP namespaces different compared to C++? From: theanomaly.is@gmail.com (Sherif Ramadan) 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.