tags:

views:

140

answers:

6

I come from Java / Objective-C to PHP and find it horrible. I mean, PHP is nice, really. But when you look at a bunch of variables, you don't know: Is that an number? Is that an string? Or ist that even a fancy object that can perform actions when calling methods on it?

So I wonder if there are helpful re-usable naming conventions for variables to help figure out if something is an object, or if something is just a boring variable. I'd say if something is an object, i.e. an instance of a class, the first character must be BIG. But it's just a guess. Hope to read some tips from PHP pros :-)

+3  A: 

What's to prevent you from using the conventions you're already familiar with? It's likely that there is nothing unique to PHP that you've not already witnessed before.

Jonathan Sampson
+10  A: 

As in other languages, there is no single coding standard in PHP. You can, among others (see the comments), check out the Zend coding standards, they are quite highly regarded as they are very close to (and partly identical with) the PHP core development team.

Pekka
add PEAR please: http://pear.php.net/manual/en/standards.php
Gordon
I also stick to the Zend coding guidelines: camelCasedVariables , This_Class_Naming_Scheme, BIG_CONSTANTS and identifiers that actually tell you what they represent like $databaseConnection instead of $dbc.
Techpriester
A: 

I usually stick to this simple rule

Meaningfull Camel Case Variable names (with type prefix before) ex : intNbDaysLeft would be good for an integer

David Brunelle
Yuck! How'd you prefix a variable holding a `Zend_Gdata_MimeBodyString` then? :)
Gordon
When the dataType is long and complex, I usually take the first letter of each word. So you'de get something like zgmbsVariable. Some people reduce it only to the first 3 letter, or what we do is prepend an o for Object + a meaningfull name. Again, I insist on MEANINGFULL. If you understand what you are writing and other people do,then it's fine.It's all a matter of how you're used to work.
David Brunelle
Meaningful + systems hungarian? I'm pretty sure that's an oxymoron.
Johrn
A: 

I use Zend Eclipse, so I describe my variables like this, to make sure they're intellisensed. Then, I worry less about, "what is this?"

/**
 * @var Zend_Form_Element
 */
protected $_member;

or

/**
 * @param Zend_Form_Element $input
 */
function do_it( $input )

or

$my_var = some_function(); /* @var $my_var Zend_Form_Element */
Derek Illchuk
That is PHPDoc, not "Zend Eclipse convention."
chelmertz
A: 

You should quickly discover that there is little type hinting in PHP, and the reason for that is clear, in this dynamic language type can change, so name is not very important.

Using PHPDoc with Eclipse or NetBeans helps, but you cant rely on that, if you want to be sure that Your variable is of specific type, you have to check it (is_array(), instanceof, ect.).

Also, PHP does a lot for you, it will convert betwen types on Your behalf, it will act differently depending on the type.

PHP mostly works with strings, so most variables are of that type. Applications can have lots of types, but a single method shouldn't use too many. If you find yourself using so many objects that you cant easily keep track of their instance names, then it's some design problem, not naming convention issue.

If you want to be sure you have not used some variable wrongly, be sure to unit test. The standard in PHP is phpunit

PiotrN
"PHP mostly works with strings, so most variables are of that type" ... [Citation needed]
nickf
A: 

My favourite PHP naming convention is the one where “PHP” is always called “huge pile of cr— that we wouldn’t touch with a bargepole”.

Paul D. Waite
there's always one, isn't there
nickf
Uh oh, tough crowd.
Paul D. Waite
Yep, tough - but if we allow for these answers, all of SO will become one big bad joke, since there are opinions like this for every programming question.
Sam
NOES DAT IZ NOT TRUE RUBY IS DA BOMB IF STACKSOVERLFLWO WAS WRITTEN I RUBY NOONE WOULD EVER WRITEBAD STUFFS BECAUSE THE RUBY MAGICK WOULD STOP THEM LOLOL
Paul D. Waite