PHP Good Practices 1 – Naming Conventions

Target Audience:

Intermediate experienced programmers who have a reasonable understanding of Object Oriented Programming.  Anyone wanting to learn about Good Practices in programming for PHP.

Languages Used:

PHP 5+

Related Material:

PHP Manual – Class
Class Design in PHP
OOP in PHP
PHP Good Practices 2 – Self-Documenting Code

Introduction:

As a self taught programmer going on nine years in the industry I have seen a lot of great code!  But I have seen far more bad code.  Have you ever had to jump into someone else code and have to read it line by line and keep track of obscure variables before you can get a handle on what the code is even doing?  I have many times, and each time it is increasingly frustrating.

High Level programming languages and OOP were designed to provide a tool for programmers to write  human understandable descriptive language that could also be understood by a computer by running it through a compiler.  The whole concept was to have a way for humans to understand the same code that the computer does, making it easier to maintain and spread across teams.

This is a great concept when it is followed, the problem is many programmers today are not taught standards, or go to work for a company that does adopt any standards.  Suddenly it is a free for all where all programmers are doing things ‘their way’.  Although the independence is great, the productivity suffers as programmers have to maintain someone else’s code or work with someone on a team who does things way different.

That is why there is standardization guidelines for most all programming languages.  These guidelines suggest Good Practices and conventions for naming variables, functions, objects, and most every element of a programming language.  Understanding and implementing one of these sets of standards across your team is your first step towards unity, and high productivity.

This article will cover the naming conventions for PHP.  These guidelines are specific to PHP as defined by Zend with use with the Zend Framework.  Although these conventions are specific to a language, they are just guidelines and choosing one standard for all languages you support is often easier then trying to support them all.   However you choose to adobt them, the most important thing is that your whole team is doing the same thing.

Pascal Case and Camel Case?

There is  debate going on about rather it’s called ‘Pascal Case’ or ‘Upper Camel Case’.  I remember back in my high school programming class, my teacher explained to us the difference between Pascal Case and Camel Case.  According to her, Pascal Case is when the first letter of each word starts with a capital letter and their are no spaces.  Camel case is the same thing except the first letter of the word starts with a lowercase letter.

Example:

Pascal Case -> ThisIsPascalCase (Also Called Upper Camel Case)

Camel Case -> thisIsCamelCase (Also Called Lower Camel Case)

Microsoft also defines these styles as such according  to ‘Capitalization Styles‘ on MSDN.  My understanding was that Pascal Case was coined because it was a style used by many PASCAL programmers.   I have heard this debated across the internet as well.

Many programmers today are now calling, what I have known as Pascal Case, ‘Upper Camel Case’.  This seems to be fairly widely accepted outside the Microsoft world.  Regardless of what you call it, the important thing is knowing where to implement it.

Naming Conventions

Probably one of the most important thing to remember when naming your code elements is to just use common sense names.  These is no reason to get out of control but using self explanatory names in your code will make it easier for you and other programmers later.

Naming conventions are a set of ‘guidelines’ defining what case is used when naming elements of your code.  This is done to keep your code consistent, making it easier to follow and understand. The two cases used most often, and covered in this article are Pascal Case and Camel Case.

Naming conventions are often defined by the language being used.  They can also be defined at an organizational level, meaning your company could determine that all code use one convention regardless of language or platform.  The most important thing is that you understand what convention is being used, and that you follow it.

PHP Naming Guidlines – Zend Framework 1.9+

Classes – PascalCase (UpperCamelCase)

Special Considerations:  Class names must match the name of the directory on the file system that contains the class file.  Names are to be AlphaNumeric only with the exception of an ‘_’ which is only to be used to show separation in the path.  If you have a class file named Car.php in the path ‘/Models/Car.php’ then the class name would have to be Models_Car.

Interfaces – PascalCase (UpperCamelCase)

Special Considerations: See Classes.

Functions and Methods – camelCase

Special Considerations: AlphaNumeric only with exception of ‘_’ as described here. Names must describe behavior of function or method.  Methods that are declared with private or protected visibility modifier must start with an _.

Variables – camelCase

Special Considerations: Names should describe the data in the variable.   Variables declared with private or protected visibility modifiers must start with an _.

Constants – ALL_CAPS

Special Considerations:  AlphaNumeric and ‘_’ are allowed however ‘_’ must be used to separate  words in constants.

Code Example:

define("CURRENT_YEAR", date('Y'));

interface Transport
{
    public function getDescription();
}

class Models_Car implements Transport
{
    private $_make = 'Ford';
    private $_model = 'Mustang';
    private $_year = 1967;

    private function _findAge()
    {
       return CURRENT_YEAR - $this->_year;
    }

    public function getDescription()
    {
        $desc = $this->_year . ", " .
        $this->_make . " " .
        $this->_model . " is " .
        $this->_findAge() . " years old.";             

        return $desc;
     }
}

$car = new Models_Car();
echo $car->getDescription();

— Live, Learn, Share —
Helpful Books: