When to use namespaces

When to use namespaces

am 01.03.2010 17:14:21 von Auke van Slooten

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:
\\

(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

Re: When to use namespaces

am 01.03.2010 17:25:09 von Ashley Sheridan

--=-89OJlA6Xgup0xDySbWl6
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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:
> \\
>
> (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



--=-89OJlA6Xgup0xDySbWl6--

Re: When to use namespaces

am 01.03.2010 17:59:40 von Richard Quadling

On 1 March 2010 16:14, 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 practic=
e,
> but most of the pages dealing with namespaces start with the assumption t=
hat
> you are building a complex application with lots of modules and say thing=
s
> like:
>
> Namespaces should be all lowercase and must follow the following conentio=
n:
>   \\
>
> (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 t=
hat
> 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 versi=
on
> 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
>
>

If you are intending to document your code using DocBlocks and then
build a manual using phpDocumentor, then the current version of
phpDocumentor does NOT support namespaces.

Add to that the 5.3.0+ issue, then maybe sensible prefixes may be the
easier choice for the time being.

--=20
-----
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=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

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

Re: When to use namespaces

am 01.03.2010 20:21:15 von TedD

At 5:14 PM +0100 3/1/10, 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:
> \\
>
>(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)


Tie it to your url, such as:

ripcord.muze.nl

That would be sufficient and unique as a namespace under your control.

Cheers,

tedd


--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

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

Re: When to use namespaces

am 01.03.2010 20:34:29 von Adam Richardson

--001636025ff07092750480c25633
Content-Type: text/plain; charset=ISO-8859-1

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 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:
> \\
>
> (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

--001636025ff07092750480c25633--

Re: When to use namespaces

am 02.03.2010 12:18:44 von Richard Quadling

On 1 March 2010 19:34, Adam Richardson wrote:
> I use namespaces within my web framework because the framework takes a mo=
re
> functional approach (no objects are created within the framework other th=
an
> from existing classes such as PDO or Exception, immutability is promoted,
> etc.), and in this context, the namespaces felt quite natural for breakin=
g
> up the various groupings of functions.
>
> My framework does require PHP 5.3, and, honestly that has caused some pai=
n
> 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 fre=
quent
> static method calls, maybe it's worth it.  For instance, if you've u=
sed 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 stren=
gths :)
>
> Adam
>
> On Mon, Mar 1, 2010 at 11:14 AM, 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 bette=
r
>> 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 practi=
ce,
>> 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 thin=
gs
>> like:
>>
>> Namespaces should be all lowercase and must follow the following conenti=
on:
>>   \\
>>
>> (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 s=
o,
>> 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 (
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.



--=20
-----
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=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

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

Re: When to use namespaces

am 03.03.2010 06:32:41 von Adam Richardson

--001636e0a62b9f6a270480decf8f
Content-Type: text/plain; charset=ISO-8859-1

Thanks, Richard, I wasn't aware of that function. Another nice option.

On Tue, Mar 2, 2010 at 6:18 AM, Richard Quadling
wrote:

> On 1 March 2010 19:34, Adam Richardson 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 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:
> >> \\
> >>
> >> (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 ( >
> 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
>



--
Nephtali: PHP web framework that functions beautifully
http://nephtaliproject.com

--001636e0a62b9f6a270480decf8f--