Principles of class naming

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
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Principles of class naming

Post by kaisellgren »

I'm writing a huge project that involves lots of classes. I'm already using classes like zip, security, http, service, etc so I am wondering how I should name these classes. (While it's still an easy job.)

For example, I won't call my zip class a "zip" since it would probably get intefered by PHP in the future. What about other names such as "security","http","service","string",etc? How do you name your classes, do you use a prefix, is using a prefix way to go?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Principles of class naming

Post by papa »

I try to name my classes as close (and descriptive) to what I'm doing as possible: user_auth (User authentication class) is pretty self explanatory for example, instead of maybe name it authenticate and confuse myself later on when i want to make authentication for something else. Sometimes I use prefix, MySQL_connect for example.

Would be interesting to see how other people name their classes though.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Principles of class naming

Post by Eran »

Use your project name as the prefix, that should prevent future collisions
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Principles of class naming

Post by papa »

pytrin wrote:Use your project name as the prefix, that should prevent future collisions
What if you have 2 zip classes that does different things?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Principles of class naming

Post by Eran »

They should have different names according to what they do - and probably extend the same zip base class

Code: Select all

class Project_Zip {
   ...
}
 
class Project_Zip_Archive extends Project_Zip {
  ...
}
 
class Project_Zip_Memory extends Project_Zip {
  ...
}
User avatar
VirtuosiMedia
Forum Contributor
Posts: 133
Joined: Thu Jun 12, 2008 6:16 pm

Re: Principles of class naming

Post by VirtuosiMedia »

I've more or less modeled my classes after the Zend naming schema.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Principles of class naming

Post by kaisellgren »

pytrin wrote:They should have different names according to what they do - and probably extend the same zip base class

Code: Select all

class Project_Zip {
   ...
}
 
class Project_Zip_Archive extends Project_Zip {
  ...
}
 
class Project_Zip_Memory extends Project_Zip {
  ...
}
That's exactly what I'm doing. I'm separating payment gateways and using a base abstract class called "payment".

So do you think I should go for a prefix?

Code: Select all

class ph_http {}
abstract class ph_security {}
abstract class ph_payment {}
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Principles of class naming

Post by Eran »

Yep. It's the common namespacing tactic without actual namespaces (coming in PHP 5.3)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Principles of class naming

Post by josh »

Group classes together into packages, classes should be loosely coupled yet highly cohesive. Naming is everything. Often a trivial name changes causes me to have a series of breakthroughs in looking at the design in a different light. Naming is actually a really involved process so maybe if you provided some context I could give a more descriptive answer.

For instance imagine you were just dropped into a 3rd world country and you saw a rabbit run by, one of the natives yells out "gordiva", does gordiva=rabbit? furry animal? scurring creature? does it refer to the ground the rabbit is running on, the air that was displaced by the rabbit moving in space? does it refer to the plants next to the rabbit?*

As you can see language is actually exceedingly complex. Names should be entirely unambiguous and clear. You should be able to explain your design to non-programmers...

- Josh

*example shamelessly stolen from Steven Pinker
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Principles of class naming

Post by Jenk »

Accurate and descriptive naming. Name collisions? Don't care. Use namespaces if collisions bother you.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Principles of class naming

Post by kaisellgren »

Jenk wrote:Accurate and descriptive naming. Name collisions? Don't care. Use namespaces if collisions bother you.
I don't want to require PHP 5.3 just yet :P

@Josh: Right now I'm using short and describtive names for my classes and they use the same one prefix. I do have an online documentation explaining the purpose of each classes. I hope that makes sense.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Principles of class naming

Post by josh »

That's actually really excellent. If you want to get better I'd learn to divide stuff into packages / control dependencies, etc... Domain Driven Design is a good book which focuses a lot on the skill of naming things. Also OO models the way our mind categorizes things, if you're really into it like me you'll be very interested in learning about linguistics and nuero science.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Principles of class naming

Post by kaisellgren »

jshpro2 wrote:That's actually really excellent. If you want to get better I'd learn to divide stuff into packages / control dependencies, etc... Domain Driven Design is a good book which focuses a lot on the skill of naming things. Also OO models the way our mind categorizes things, if you're really into it like me you'll be very interested in learning about linguistics and nuero science.
Thanks, are you talking about this book http://books.google.fi/books?id=7dlaMs0SECsC ?

I'm not sure if I really want to look at linguistics or nuero science, but I'll jot them down for the future just in case. ;)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Principles of class naming

Post by josh »

Yes that book. I actually found out about the Steven Pinker books because Eric Evans cites them in his bibliography, if you read DDD you'll get the general idea.. just if you're a nut like me you trace stuff back to it's source to try and get the full understanding
Post Reply