NYCPHP Meetup

NYPHP.org

[nycphp-talk] Namespaces

Guilherme Blanco guilhermeblanco at gmail.com
Thu Jan 10 13:11:29 EST 2008


In a few words, one namespace is a way to organize code into scopes.
By doing this, you prevent collisions when mixing libraries.

One example...

Packages Creole, Doctrine and PEAR_DB. All of them has one class named
Connection (Ok, I know they already differ in names, but this is just
one example).
Instead of control the class name by prefixing it the lib (like
Doctrine_Connection, Creole_Connection, DB_Connection), you can
declare your classes normally (Connection to all) and use them without
care about conflicts.

This organization brings to you less letters to type, preventing you
to call for example... Doctrine_Collection_Iterator_Expandable and
just care about Expandable in your calls, by importing the
Doctrine::Collection::Iterator namespace.

Also, the entire discussion of "single" namespace is wrong.
What is being discussed there is the possibility to define only one
namespace per file or more then that. I am +1 in more than 1, but it
has some serious performance issues by doing this. Currently, this is
impossible in PHP:

namespace MyNS;

class Foo {}

class Bar {}

namespace MyNS2;

class Foo2 {}

In this case, you should move the MyNS2 into a different file. This is
bad, since there are some libraries (example: Doctrine) that has a
compiler which compiles the entire source into a single file. Doctrine
is the best ORM for PHP today, and it has more than 100 possible
namespaces. Compiler then is unable to pack into a single file if it
is ported to 5.3.


Also, ASP (read as .NET) allows you to define inner namespaces.

namespace MyNS {
    namespace SubNS1 {
        class Foo {}
        class Bar {}
    }

    class Foo2 {}
}

PHP does not have this support directly. You should do this:

file1.php

namespace MyNS;

class Foo2 {}



file2.php

namespace MyNS::SubNS1;

class Foo {}

class Bar {}




Everything evolving namespaces in PHP is a delicate subject and must
be dealt with care. Dev team is working hard to improve performance,
and allowing some constructions that users want can slow down
execution (since it'll move from compile time into runtime).

Is everything clear for you now? If not, just reply to message and I
can answer, without any problem.... =)



Regards,

On Jan 10, 2008 3:38 PM, David Krings <ramons at gmx.net> wrote:
> Hi!
>
> I got sucked into a (probably pointless) discussion about ASP vs. PHP. One of
> the arguments against using PHP was that there is only a single namespace.
> Uhm, that took the wind out of my sail as I have no clue what a namespace is
> and why having only one is really bad.
>
> Anyone can explain that to me in laymen terms?
>
> David
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>



-- 
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9166-6902
MSN: guilhermeblanco at hotmail.com
URL: http://blog.bisna.com
São Carlos - SP/Brazil


More information about the talk mailing list