Namespaces and abstract class names

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
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Namespaces and abstract class names

Post by Christopher »

Using the PEAR/Zend convention for class naming an abstract class might be called Foo_Bar_Abstract. The problem is that if you change to namespaces then you end up with this naming problem:

Code: Select all

namespace Foo\Bar;

abstract class Abstract {
Obviously "abstract" is a keyword. I noticed that ZF2 has come up with the clunky convention of naming the class \Foo\Bar\BarAbstract. I assume the same problem applies with interfaces.

I was discussing this with Jonah Bron and wondered what people thought the best naming scheme might be? I think part of my uneasiness at using "Abstract" in class names is that is is similar to adding the type to variable names. Maybe not exactly the same, but similar. I'd prefer the name to be expressive of what it does and not all parent classes have exactly same intent or usage. Some may be a true abstract class that mix unimplemented, required methods with implemented methods. Other parent classes are more a library of methods that the child may use, but maybe you don't consider those truly abstract. It can be a fine line between a class used as a parent and a class declared as abstract. So does naming these classes using terms like Base or Common make more sense?
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Namespaces and abstract class names

Post by josh »

'Abstract' is already redundant because you typed it before the class name, in this case I'd go with 'Foo_Bar' as the abstract class itself, if that's already taken re-name it. (Foo_Bar_Implementation ?? Think of something more descriptive is better).

I think this mirrors real world objects. In real life you don't just instantiate a House, you can't even look at the whole house at once, its sort of abstract in a sense. So I would just have /House be your abstract class. /House/Wall, and House/Door, and /House/Nails and such would be your 'concrete' objects. Or perhaps /House is your container, which contains a couple /House/Room objects, and /House extends /House/Foundation?

Either that or use a domain specific term, like the /House/Foundation is your abstract class, or the /House/Blueprint. Some will say this is an oversight of PHP others will argue its going to promote better naming. I'd rather be in the code that calls it a house/blueprint than a house/abstract. To most people the word 'abstract' is very abstract. Confusing.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Namespaces and abstract class names

Post by VladSun »

Although I use namespaces, I haven't faced such problem yet. I always use Abstract for the abstract classes. Usually, I use single word namespaces - i.e.

Code: Select all

namespace Foo;
and almost never

Code: Select all

namespace Foo\Bar;
IMHO, namespaces should not be used as part of the class naming - its purpose is to solve naming conflicts.

Also, I don't think having an abstract class in the root of the object hierarchy makes sense - it is an abstraction of what? Sounds like it's an abstraction of the namespace itself.
There are 10 types of people in this world, those who understand binary and those who don't
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Namespaces and abstract class names

Post by josh »

VladSun wrote:the root of the object hierarchy
I don't consider Foo_Bar any more/less special than Foo_Bar_Foo. My classes accept that they're not the center of the universe ;-)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Namespaces and abstract class names

Post by Jenk »

Or get rid of the "abstract" bit of it, and use it as a composite in all "child" classes :mrgreen:

I am seriously of the opinion these days that abstract classes are foul, and the only thing that should ever be abstracted is an interface (which is redundant in PHP anyway, as of course it is dynamic - happy days!)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Namespaces and abstract class names

Post by VladSun »

Even if I call it Abstract, it's not always declared as such. It's the "base" class.

And if its only interface, I use Interface. If one can put some code to reuse, it's a "base" class.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Namespaces and abstract class names

Post by Jonah Bron »

josh wrote:I think this mirrors real world objects. In real life you don't just instantiate a House, you can't even look at the whole house at once, its sort of abstract in a sense. So I would just have /House be your abstract class. /House/Wall, and House/Door, and /House/Nails and such would be your 'concrete' objects. Or perhaps /House is your container, which contains a couple /House/Room objects, and /House extends /House/Foundation?
The problem with that is that it leaves the base class outside of the namespace in the cold :cry:
VladSun wrote:Although I use namespaces, I haven't faced such problem yet. I always use Abstract for the abstract classes. Usually, I use single word namespaces - i.e.
But I just tried running "abstract class Abstract{}" in PHP interactive shell, and it threw an error. Maybe that's not what you meant?
VladSun wrote:IMHO, namespaces should not be used as part of the class naming - its purpose is to solve naming conflicts.
In light of that, how do you think namespaces should be implemented in the context of a framework?
VladSun wrote:Also, I don't think having an abstract class in the root of the object hierarchy makes sense - it is an abstraction of what? Sounds like it's an abstraction of the namespace itself.
Do you mean it should be inside the namespace? Then I agree.
Jenk wrote:I am seriously of the opinion these days that abstract classes are foul, and the only thing that should ever be abstracted is an interface (which is redundant in PHP anyway, as of course it is dynamic - happy days!)
Why exactly? What if you have common functionality that doesn't quite work as a concrete class?
VladSun wrote:And if its only interface, I use Interface. If one can put some code to reuse, it's a "base" class.
Christopher suggested "Base" earlier in our discussion. It seems like a good solution.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Namespaces and abstract class names

Post by Jenk »

To answer without quoting, I've not had that situation occur. Ever. And even struggle to think of an example. :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Namespaces and abstract class names

Post by VladSun »

Jonah Bron wrote:
VladSun wrote:Although I use namespaces, I haven't faced such problem yet. I always use Abstract for the abstract classes. Usually, I use single word namespaces - i.e.
But I just tried running "abstract class Abstract{}" in PHP interactive shell, and it threw an error. Maybe that's not what you meant?
No, I didn't meant that. I said (after that) I had never declared an abstract class in the "root" directory. I do follow Zend/PEAR class naming convention.
Jonah Bron wrote:
VladSun wrote:IMHO, namespaces should not be used as part of the class naming - its purpose is to solve naming conflicts.
In light of that, how do you think namespaces should be implemented in the context of a framework?
I think the namespace must be single-word - the name of the framework :) Just like Zend and ZendX exist... IMHO, a namespace should be an unique identifier. I remember Java classes can be put in "com.provider.class....." etc. "namespace".
Jonah Bron wrote:
VladSun wrote:Also, I don't think having an abstract class in the root of the object hierarchy makes sense - it is an abstraction of what? Sounds like it's an abstraction of the namespace itself.
Do you mean it should be inside the namespace? Then I agree.
I didn't understand that one, sorry
Jonah Bron wrote:
VladSun wrote:And if its only interface, I use Interface. If one can put some code to reuse, it's a "base" class.
Christopher suggested "Base" earlier in our discussion. It seems like a good solution.
[/quote]
Well, I don't use abstract modifier because it's harder to test the class. Indeed it's an "Abstract Base" class.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Namespaces and abstract class names

Post by Jonah Bron »

If you're looking for an example, here's one.

https://code.google.com/p/skeleton/sour ... k%2FA%2FDb
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Namespaces and abstract class names

Post by Jenk »

I'm struggling to see how that can't be a composite.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Namespaces and abstract class names

Post by VladSun »

Jenk wrote:To answer without quoting, I've not had that situation occur. Ever. And even struggle to think of an example. :)
I think there should be a balance (I would say max 30% inheritance and min 70% somposition).
Just think - there isn't a single framework (well known) that doesn't use inheritance.
Last edited by VladSun on Tue Feb 01, 2011 12:20 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Namespaces and abstract class names

Post by Jenk »

My opinion on that is because it is more difficult to enforce behaviour through composition than it is through inheritance.

Composition is difficult to do, I am fully aware. However it is very easy to do inheritance badly, but just as difficult to do cleanly.

Note that there isn't a "composition is easy to do badly" in that statement.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Namespaces and abstract class names

Post by Jonah Bron »

VladSun wrote:No, I didn't meant that. I said (after that) I had never declared an abstract class in the "root" directory. I do follow Zend/PEAR class naming convention.
I see now.
VladSun wrote:I think the namespace must be single-word - the name of the framework :) Just like Zend and ZendX exist... IMHO, a namespace should be an unique identifier. I remember Java classes can be put in "com.provider.class....." etc. "namespace".
Hm. And it's child class would still use underscores to separate packages?
VladSun wrote:I didn't understand that one, sorry
It's okay, you already answered it.
Jenk wrote:I'm struggling to see how that can't be a composite.
What do you mean by that? What's a composite?
VladSun wrote:I think there should be a balance (I would say max 30% inheritance and min 70% somposition).
Just a think - there isn't a single framework (well known) that doesn't use inheritance.
I'm sure I'll understand what that means as soon as I know what a "composite" is... :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Namespaces and abstract class names

Post by John Cartwright »

Composition is when one object directly instantiates a new object for it's own purposes (directly coupled).

I.e.,

Code: Select all

class Foo
{
   protected $_bar;

   public function __construct()
   {
      $this->_bar = new Bar();
   }

   public function doSometing()
   {
      $this->_bar->foobar();
   }
}
Post Reply