7

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?

Rob
  • 47,999
  • 5
  • 74
  • 91
alex
  • 479,566
  • 201
  • 878
  • 984

8 Answers8

14

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
  • 2,381
  • 1
  • 16
  • 16
  • 1
    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 Dec 12 '08 at 04:41
  • Disagree, see [this](http://stackoverflow.com/a/1557799/1870054), they talk about the global variables,they are not always bad. – wener Dec 08 '13 at 06:31
2

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
  • 7,752
  • 5
  • 39
  • 60
1

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.

Update on 10 year anniversary:

These days, there is a standard, which is largely accepted within the community. You should stick with that:

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md

troelskn
  • 115,121
  • 27
  • 131
  • 155
0

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
  • 4,073
  • 1
  • 24
  • 23
0

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
  • 88,666
  • 34
  • 128
  • 138
0

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

0

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
  • 3,105
  • 20
  • 18
0

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.

Powerlord
  • 87,612
  • 17
  • 125
  • 175