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?

8 Answers 8

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.

2
  • 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, 2008 at 4:41
  • Disagree, see this, they talk about the global variables,they are not always bad.
    – wener
    Dec 8, 2013 at 6:31
3

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.

0
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

1
  • Link is broken.
    – mmcglynn
    Aug 22, 2018 at 15:33
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.

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.

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.