Namespace naming/directory conventions

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Namespace naming/directory conventions

Post by Luke »

Back before there were namespaces in PHP, when I wanted an abstract class and then many concrete classes based on that abstract class, I would do something like this:

Code: Select all

/lib
    /Reader
        Abstract.php
        File.php
        DirectInput.php
        Url.php
Now I am using namespaces and I can do away with MyLibrary_Reader_Abstract, MyLibrary_Reader_File, etc. and just have MyLibrary\Reader\File, but what is the convention for the abstract class name? What do you guys do? Do you have a class named MyLibrary\Reader\Abstract? For some reason that just seems weird to me. I would prefer MyLibrary\Reader as the abstract class, but then the abstract class would sit in the /lib directory rather than the /lib/Reader directory where it makes more sense.

So anyway, does my question make sense? If it doesn't I'll try to explain it better. Any help is appreciated.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Namespace naming/directory conventions

Post by Celauran »

My abstract or super classes sit one level above their child classes specifically so I can have \namespace\Foo and \namespace\Foo\Bar where Bar is a child of Foo
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Namespace naming/directory conventions

Post by requinix »

Generally I'll go with MyLibrary\Reader as the base class and MyLibrary\Reader\* as the derived classes, because those classes are specialized instances of the "Reader" concept. Occasionally though I'll have an interface as the "main" class and then use an abstract class as a partial, common-functionality implementation - a base - for other classes, in which case I might go with MyLibrary\IReader and MyLibrary\Reader\ReaderBase.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Namespace naming/directory conventions

Post by Weirdan »

\MyLibrary\Reader\AbstractReader
Do you have a class named MyLibrary\Reader\Abstract? For some reason that just seems weird to me.
That wouldn't work anyway, as 'abstract' is a keyword. 'abstract class Abstract' is a syntax error.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Namespace naming/directory conventions

Post by Luke »

Good point, Weirdan. I went with this:

Code: Select all

/lib
    /MyLibrary
        /Reader
            StringReader.php
            FileReader.php
        Reader.php (abstract)
        OtherClass.php
        Foo.php
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Namespace naming/directory conventions

Post by Christopher »

Weirdan wrote:That wouldn't work anyway, as 'abstract' is a keyword. 'abstract class Abstract' is a syntax error.
Yes. Use Base or AbstractReader depending on the context and you preference.
(#10850)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Namespace naming/directory conventions

Post by VladSun »

The OP instantly reminded me of viewtopic.php?f=19&t=127265
:)

Cheers
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Namespace naming/directory conventions

Post by Christopher »

Ahhhhh ... 2011 and the Inheritance vs Composition discussions! I couldn't read more than a few pages ... but I am sure that Jenk was right! :drunk:
(#10850)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Namespace naming/directory conventions

Post by VladSun »

<offtopic>
Christopher wrote:Ahhhhh ... 2011 and the Inheritance vs Composition discussions! I couldn't read more than a few pages ... but I am sure that Jenk was right! :drunk:
Yeah, 2011 ... I am sooo tempted to wake up this thread ...
traits

:twisted: :twisted: :twisted:
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Namespace naming/directory conventions

Post by Christopher »

Never have so many, argued so long, about so many things they basically agree on!!! ;)
(#10850)
Post Reply