PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Jul 07, 2020 1:55 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 139 posts ]  Go to page 1, 2, 3, 4, 5 ... 10  Next
Author Message
PostPosted: Tue Feb 01, 2011 2:26 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
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:
Syntax: [ Download ] [ Hide ]
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)


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 5:35 am 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
'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.


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 6:16 am 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
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.
Syntax: [ Download ] [ Hide ]
namespace Foo;

and almost never
Syntax: [ Download ] [ Hide ]
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


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 7:18 am 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 11:20 am 
Offline
DevNet Master
User avatar

Joined: Mon Sep 19, 2005 6:24 am
Posts: 3587
Location: London
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!)


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 11:24 am 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
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


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 11:57 am 
Offline
DevNet Master
User avatar

Joined: Thu Mar 15, 2007 6:28 pm
Posts: 2765
Location: Redding, California


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 12:41 pm 
Offline
DevNet Master
User avatar

Joined: Mon Sep 19, 2005 6:24 am
Posts: 3587
Location: London
To answer without quoting, I've not had that situation occur. Ever. And even struggle to think of an example. :)


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 12:53 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 12:57 pm 
Offline
DevNet Master
User avatar

Joined: Thu Mar 15, 2007 6:28 pm
Posts: 2765
Location: Redding, California
If you're looking for an example, here's one.

https://code.google.com/p/skeleton/sour ... k%2FA%2FDb


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 1:00 pm 
Offline
DevNet Master
User avatar

Joined: Mon Sep 19, 2005 6:24 am
Posts: 3587
Location: London
I'm struggling to see how that can't be a composite.


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 1:04 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Last edited by VladSun on Tue Feb 01, 2011 1:20 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 1:07 pm 
Offline
DevNet Master
User avatar

Joined: Mon Sep 19, 2005 6:24 am
Posts: 3587
Location: London
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.


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 1:11 pm 
Offline
DevNet Master
User avatar

Joined: Thu Mar 15, 2007 6:28 pm
Posts: 2765
Location: Redding, California


Top
 Profile  
 
PostPosted: Tue Feb 01, 2011 1:21 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Composition is when one object directly instantiates a new object for it's own purposes (directly coupled).

I.e.,

Syntax: [ Download ] [ Hide ]
class Foo
{
   protected $_bar;

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

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 139 posts ]  Go to page 1, 2, 3, 4, 5 ... 10  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group