PHP Code Standard and Name Conventions

Today’s web development requires a lot of know-how and a fair amount of knowledge about various languages. Creating a web page isn’t as easy as before. Now you need to handle graphics, flash, CSS, JavaScript, AJAX, PHP, ASP, JSP and so on. None of the web development scripts are strict concerning code standard nor name conventions. Having clean and neat code is a goal you should strive for. When working in small or larger teams it is important that other programmers can read the code you have written. You need to have comments on the tricky parts, use describing method and variable names and overall have a similar style and type of coding throughout your application or web site.

So, there are a number of positive effects when using code standard and good name conventions. Not only do other programmers easily catch up on your code you get a good looking, neat and clean code. I just said that most of the web languages doesn’t have a standard, how are you going to follow a standard that doesn’t exist? Easy, we follow a respected and well-known framework and adapting our code style so it matches.

I was going to go through each step here in the post but there is no point. All information you need to learn is easy to read about. We are going to follow the code standard and name conventions created by the Zend Framework. I found a good text explaining what the Zend Framwork is:

“Zend Framework is a simple, straightforward, open-source software framework for PHP 5 designed to eliminate the tedious details of coding and let you focus on the big picture. Its strength is in its highly-modular MVC design, making your code more reusable and easier to maintain.”

We are going to use this code standard and name conventions because it’s wildly spread and many other languages have the same or similar style. If you are new to programming you should adapt a good style and standard as soon as possible, start with this one. All references and information is found at the link below:

It is important that we stick to our name conventions and code standard throughout our application. When everyone in the team follows the same style we can easily share the code base and let everyone in on our project. It takes time to explain code to someone, follow the standard.

Naming classes, methods and variables are sometime hard. When it comes to naming methods it is hard most of the time because the method is doing too many things at the same time. I always try to separate methods and functions so they only handle one task each. If you think the code is tricky, the code is too tricky. Try to abstract more features into other new methods or see if you can use any other structure to handle the task.

This upcoming week you will see updates on all the posts for the PHP series #1 where we will adapt the Zend coding style and hopefully we will see some nice PHP unit tests too. As for now, make sure to check out the link above and start reading about how you should name your class, methods and variables and discover how much it does with clean and nice code.


About this entry