Re: When to use namespaces

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 2010-03-01 at 17:14 +0100, Auke van Slooten wrote:

> Hi everyone,
> 
> I'm doing a small hobby project to better my understanding of php5, 
> specifically php5.3 and I'm wondering when a namespaced project is 
> better and when it is better to simply use a prefix to all class names.
> 
> I've been trying to get a feeling for what is considered the best 
> practice, but most of the pages dealing with namespaces start with the 
> assumption that you are building a complex application with lots of 
> modules and say things like:
> 
> Namespaces should be all lowercase and must follow the following conention:
>     <vendor>\<package_name>\
> 
> (thats from the php.standards mailing list btw)
> 
> In my case the project is a single module, single php file, with about 6 
> classes. It is an OO wrapper for PHP's xmlrpc methods (client and 
> server) and meant to be used in a number of different projects.
> 
> Is it considered a good idea to use a namespace in such a case? And if 
> so, what should that be? I've named the project 'ripcord', and used that 
> as a namespace as well. I could probably name it 'muze.ripcord', but 
> somehow that feels less 'open' to me.
> 
> Thanks in advance for any thoughts,
> Auke van Slooten
> Muze (www.muze.nl)
> 
> PS. The project is at http://code.google.com/p/ripcord/, the PHP5.3 
> version is at 
> http://code.google.com/p/ripcord/source/browse/#svn/branches/php5.3
> 


To me, namespaces are primarily to avoid clashes with other classes of
the same name. I guess if your class has a really distinctive name, then
namespaces might be less important. If your class had a more generic
type of name then you should use namespaces to avoid collisions.

Having said that, by using namespaces, you do limit your app to
installations of PHP to that of >=5.3.0. Some shared hosting solutions
may not offer 5.3.0 (and I know one that still favours php 4!) so it may
be that using another mechanism for avoiding class name collision might
be better in the short term.

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux