PHP, Classes, Camel Case, and Underlines

Wednesday, November 18th 3:40pm Matt

When I first started PHP programming, I wrote code like the PHP inventors wrote code, using lowercase characters and underlines, like:

$my_favorite_variable = 5;

function add_numbers($first_number, $second_number)
{
    return $first_number + $second_number;
}

$answer = add_numbers($my_favorite_variable, 1);

But this became tiresome. The thing is, the underscore character is harder to type and using all lowercase really makes the code blur together at a glance. Also, it’s harder to tell the difference between variables and function names. After writing code at Microsoft for many years, I know that camel case is the way to go. Even Apple uses camel case. Modern C programming uses camel case. The PHP inventors made a mistake when using all those underscores.

Using camel case and adding in classes, the code becomes so much cleaner:

$myFavoriteVariable = 5;

class Calculator
{
    public static function Add($firstNumber, $secondNumber)
    {
        return $firstNumber + $secondNumber;
    }
}

$answer = Calculator::Add($myFavoriteVariable, 1);

Doesn’t that look better? We’re at this point in history where we’ve pretty much figured out that for all languages camel case it the easiest to read. Start variables with lowercase, classes and methods with uppercase and it looks so nice.

Having said that, every time I start to work on something I haven’t touched in a while there’s a flurry of code refactoring, but it feels so good to get that done. And it’s so much easier to read.

Submit a Comment