views:

34

answers:

6

I was wondering what is better or more accepted when coding in PHP. I was taught, in Java, that class methods to get and set variables should be prefixed with "get" and "set". What I want to know, though, is should I use these prefixes on regular PHP functions.

For example, to retrieve a username from a session variable I would either have
getUsername()
or
username()

What are the advantages and best practices. I know that using "get" is more mnemonic but it's rather redundant (especially for a personal project that I don't expect to be having other people read) but for the sake of good practice I would like to get it right.

While I'm at it, what is the proper naming convention for variables? Underscore-separated or camel-case? I've looked around and I've seen a mix of both. Wordpress tends to use underscores in their function names but a lot of other sites say camel-case is best.

A: 

Personally, I think the more explicit and easy to interpret the method and variable names are the better. This is very important if anybody other than you will look at the code. It also helps as the project grows larger.

The other key is consistency...pick something and stick with it.

Peter Loron
+1  A: 

Personally I find the "get" prefix helpful. Though it depends on your IDE and preference. If your IDE provides comments and tooltips, then it can remind you with a simple mouseover that myFunction does this or that.

There are other useful prefixes that I frequently use: get, set, validate (or just val), parse, and show. I find that in a sea of functions, the little descriptors help greatly.

JYelton
I'm using TextMate on Mac OS X, I don't think it has any mouse-over definition features.
alexcoco
+1  A: 

first of all $_SESSION is a global variable, you really do not need a getter for it. It is better to use getUsername() as per better naming convention.

Rest is personal choice or opinion. It's better to choose a naming convention which suits you and stick to it.

Aman Kumar Jain
+1  A: 

While I'm at it, what is the proper naming convention for variables? Underscore-separated or camel-case?

Whatever you like best as long as your consistent with it. The underscore style is more old-school and C like while the camelcase style is obviously inspired by Java. Seperating with underscore makes your names longer though.

Raoul Duke
+2  A: 

I personally try to stay away from getters/setters. I prefer to use the magic methods so then I can do $foo->myVar without needing to explicitly call a function (I think it makes the code more readable).

With that said, there are circumstances where I use explicit getters and setters (Basically in situations where the result would be ambiguous using the variables directly. Like if I map the magic methods to an internal array and have other member variables that need accessing). In those circumstances, I use $foo->getMyVar() as the function signature.

IMHO, $foo->myVar() makes sense if you know it's a variable. But what happens if you see $foo->show(). Does that mean to perform the show action? Or does that mean get the current setting for the show variable?

I always try to name all methods with something that identifies what they do. $foo->var() doesn't give any indication to what's going on. But $foo->showVar() does (At least moreso).

As for naming convention, it's all about who you read. I prefer camel-case. But there's nothing wrong with underscores. Pick one, and stick with it. Consistency is more important than the choice itself...

ircmaxell
+1  A: 

In general, it's good for a method to contain a verb.

Check out the PEAR naming conventions for PHP: http://pear.php.net/manual/en/standards.naming.php

They encourage the "get" prefix for getter methods. I'd amend this to say to use "is" as the prefix for boolean class properties.

PEAR also encourages camel-case (or "studly caps") instead of underscores.

Bill Karwin