Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62879 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 93889 invoked from network); 7 Sep 2012 00:21:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Sep 2012 00:21:04 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.42 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.215.42 mail-lpp01m010-f42.google.com Received: from [209.85.215.42] ([209.85.215.42:60932] helo=mail-lpp01m010-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E9/DF-03079-FED39405 for ; Thu, 06 Sep 2012 20:21:04 -0400 Received: by lahl5 with SMTP id l5so1554203lah.29 for ; Thu, 06 Sep 2012 17:21:00 -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=C1D8lsAycBViUPEA8OWZi3meFR7ld1aPHDUjDNB3X0I=; b=bYCoA1hsqZ1QxTmuM+PciTczinintrAQWponH04GqtPJLLpehn5jHl0YrBnrlswNHi yubkdMlJMeCT6d9hJOO7c3NkPnKYvP4HpKLo8oxp4gSKTaT2E518gr/V8SRETj9Zh7qA DajfaKvujS76+TC/CQh2FoOdSpb/Zv1Aegydkri1PqRnnJHJyP5SPAX8R+zwSxAZ2won ZxmP8/Y0HV48QyuPej+R3vC7erLZ1xZAk6mCuzIWE79TTGBnpABSmbE3dfmcu7NSXxeZ OuTRdm+zj8Xt4gQXe9l12a518tfCw5TtMjT6nl47A13CRAPcA7fbzgTUaUi9Td7ihAga f1gA== MIME-Version: 1.0 Received: by 10.112.9.3 with SMTP id v3mr1504687lba.32.1346977260556; Thu, 06 Sep 2012 17:21:00 -0700 (PDT) Received: by 10.112.48.39 with HTTP; Thu, 6 Sep 2012 17:21:00 -0700 (PDT) In-Reply-To: References: Date: Thu, 6 Sep 2012 18:21:00 -0600 Message-ID: To: Sherif Ramadan Cc: Mark , 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: morrison.levi@gmail.com (Levi Morrison) 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.