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:

7 thoughts on “PHP Good Practices 1 – Naming Conventions

    1. Zend’s guidelines say variables should use camelCase. The only special consideration with variable naming is when declaring private variables, then Zend recommends using an ‘_’ prefix so that you can quickly identify your private members of your class.

      My suggestion for local variables is to use them sparingly and only as needed. Most of your run-time data should fit into your class structure. If you are not using OOP then I would encourage you to find a way to keep your variables organized and name them something that easily identifies them.

  1. good post, I think you can take the idea a bit further by introducing variable type.
    I code my variable as follows:

    $strFirstName = ‘sonny’;
    $intAge = 5;

    anyway, just a thought.

    1. Hi Ronald, That is a good point as well. I have seen this done quite a bit in other languages. I personally like that idea when using an editor that provides auto complete functionality. Then you can quickly find all your strings by starting to type ‘str’, auto-complete should pull up any active variables that start with ‘str’ then, making them easy to locate quickly.

      I will actually be talking a bit about this in part 3 of this series when I talk more about Documentation Generators. I hope you read the next article in the series, ‘Self-Documenting Code’ which I will be publishing soon. Thanks!

  2. I’m wondering if the Hungarian Notation makes sense if the same variable changes is type. String converted to Int for example. On those cases, does Hungarian Notation makes sense?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s