views:

699

answers:

8

I have read a lot of popular standards manuals for open source PHP projects.

A lot enforce underscores for variables spaces, and a lot enforce camelCase.

Should global functions and variables be named differently to class methods/properties?

I know the most important thing is consistency, but I'd like to hear some thoughts on this.

What would you recommend?

A: 

Yes, the most important thing is consistency. If you are the lone developer, stick with a method. If you are working with a team, talk to the other team members. Differentiating between globals, functions/methods and classes will make reading the code much easier. For some people camelCase is easier than using_underlines so your team needs to discuss the options and pick a style.

Brian C. Lane
A: 

Note: I use underscores for my MySQL table_names, I use UpperCamelCase for MySQL field names:

Normally I use $lowerCamelCase for variable names and class properties, but if it contains the value from a field, I use the $UpperCamelCase field name, or if it is an array of data from a table, I'll use the $table_name. This way I can easily grep for SomeField or some_table and find everything referring to it.

You don't have to use this exact system, but being able to search for all references to a field or table is a huge benefit.

too much php
+5  A: 

I find camelCase a little more pleasant to type, because I find the underscore a bit awkward to type.

Don't use global variables.

I avoid procedural coding in PHP, I find OOP is easier to keep things organized. Besides, doesn't PHP have enough stuff in it's global namespace already?

Generally I try to stick to:

  • Classes are StudlyCaps singular or plural nouns, as appropriate: Item, Row, DB, Items.
  • Variables are lowercase nouns, singular or plural depending on what they hold: $column, $name
  • Constants are singular upper-case nouns: DEBUG, TYPE_FOO.
  • Methods are camelCase, and begin with singular verbs (get, perform, do), followed by a noun (singular or plural) describing what it operates on or returns (getThing(), getThings())

It definitely depends on what you're coding for. If I'm coding PHP or PEAR, I use camelCase. If I'm doing Python/Django, I use under_scores. If I'm writing ELisp, I use dashed-separators.

ieure
Agreed, I am in the progress of learning OO and what a static class is (to move all my string formatters etc out of the global namespace)
alex
+1  A: 

In PHP itself, almost every native function is underscore separated. Most of the PHP code examples in the documentation are underscore separated.

In most languages I think Camel or Pascal Casing is more appropriate, but I think there's clear history for using underscore separation in PHP.

danieltalsky
+1 you bring up a good point.
alex
A: 

I used to prefer to use camelCase, but for the sake of consistency in bigger applications, I have adopted CodeIgniter's style guide.

Even if you don't use their framework, you can appreciate the work that went into defining clear and comprehensive styles: http://codeigniter.com/user_guide/general/styleguide.html

+2  A: 

Zend Frameworks naming convention (Which is based on PEAR) is probably the closest you come to a standard in the PHP world. Personally, I prefer to use lowercase_underscore for variable names, but otherwise I mostly follow ZF's convention.

troelskn
A: 

My goal - whatever the specific format of the name - is adding more information. Does the name improve the understanding of the code and/or express something important?

If it does, great, then you've succeeded in it.

If the name doesn't add anything, why did you bother naming it?

I wrote on this one earlier this week:

http://caseysoftware.com/blog/useful-naming-conventions

CaseySoftware
+1  A: 

I would recommend reading the PEAR Coding Standards. Since PEAR is the official PHP Extension and Application Repository, it can be considered the language's official coding standard.

R. Bemrose