Re: When to use namespaces

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

 



On 1 March 2010 19:34, Adam Richardson <simpleshot@xxxxxxxxx> wrote:
> I use namespaces within my web framework because the framework takes a more
> functional approach (no objects are created within the framework other than
> from existing classes such as PDO or Exception, immutability is promoted,
> etc.), and in this context, the namespaces felt quite natural for breaking
> up the various groupings of functions.
>
> My framework does require PHP 5.3, and, honestly that has caused some pain
> in some situations to get that supported.
>
> However, using namespaces does offer some flexibility that naming
> conventions can't.  If the objects in your framework make use of frequent
> static method calls, maybe it's worth it.  For instance, if you've used the
> naming convention, you might have to call a static method like below:
>
> App_Util_DB_Query::Insert();
>
> Namespaces allow you to shorten subsequent calls, such as:
>
> use App\Util\DB\Query as Query;
>
> Query::Insert();
>
> Just a quick couple thoughts on the decision.  Both have their strengths :)
>
> Adam
>
> On Mon, Mar 1, 2010 at 11:14 AM, Auke van Slooten <auke@xxxxxxx> 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
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
>
> --
> Nephtali:  PHP web framework that functions beautifully
> http://nephtaliproject.com
>

Something I came across which has been useful in removing the long
names (<php5.3 namespaces), is the use of class_alias().

My specific use is in DocBlocks for SOAP services. Using the Zend
AutoLoader, my classes are structured so that
Namespace_Package_Class_Exception is available in
\includes\Namespace\Package\Class\Exception.php

But having those long names in the SOAP WSDL file were a bit
cumbersome. So by having a class_alias in the important class files
(just after the class definition), I could use the alias names in the
docblock and all is well.



-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[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