Page 1 of 1

Namespace naming/directory conventions

Posted: Sat Feb 15, 2014 4:12 am
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.

Re: Namespace naming/directory conventions

Posted: Sat Feb 15, 2014 8:45 am
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

Re: Namespace naming/directory conventions

Posted: Sat Feb 15, 2014 6:39 pm
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.

Re: Namespace naming/directory conventions

Posted: Sun Feb 16, 2014 8:14 am
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.

Re: Namespace naming/directory conventions

Posted: Mon Feb 17, 2014 3:12 pm
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

Re: Namespace naming/directory conventions

Posted: Tue Feb 18, 2014 5:27 pm
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.

Re: Namespace naming/directory conventions

Posted: Thu Feb 20, 2014 3:06 pm
by VladSun
The OP instantly reminded me of viewtopic.php?f=19&t=127265
:)

Cheers

Re: Namespace naming/directory conventions

Posted: Thu Feb 20, 2014 5:06 pm
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:

Re: Namespace naming/directory conventions

Posted: Thu Feb 20, 2014 5:32 pm
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:

Re: Namespace naming/directory conventions

Posted: Sat Feb 22, 2014 11:43 am
by Christopher
Never have so many, argued so long, about so many things they basically agree on!!! ;)