Re: Why are the PHP namespaces different compared to C++?
| From: | Levi Morrison | Date: | Fri, 07 Sep 2012 00:21:00 +0000 |
| Subject: | Re: Why are the PHP namespaces different compared to C++? | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62879@lists.php.net to get a copy of this message | ||
On Thu, Sep 6, 2012 at 6:15 PM, Sherif Ramadan <theanomaly.is@gmail.com> wrote:
> On Thu, Sep 6, 2012 at 5:30 PM, Mark <markg85@gmail.com> 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
>>
>> <?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.