Home » General Discussion, PHP/MySQL

PHP Developer Best Practices

8 October 2008 | 10,480 views | 4 Comments

Start using version control

Version control is like a big UNDO button for your coding. You can go back to your previous code revisions and can compare/rollback to specific code areas anytime you see necessary. It will keep track of all your changes and will empower you to track your development changes across your work/team. Also, in a distributed development team, version control helps prevent overwriting of code by team members and keeps all the members code up to date.

Subversion (SVN) is one of the most popular open source version control system. If you’re on windows platform, you can try TortoiseSVN, a client for SVN.

Use Object Oriented Programming when useful

Not surprisingly, object oriented programming is focused around objects. While the idea of objects may be foreign to you in coding terms, understanding what objects are and why we use them shouldn’t take long. In short, our non-programming world is comprised of objects. Computers, fish, clouds, people, and cars are all objects. Objects have properties like color, size, name, and speed. Objects can also be comprised of other objects: cars have doors; doors have handles; handles have plastic levers; so on and so forth. Objects are everywhere around us. For this reason, many programmers find object oriented programming relatively easy to understand.

In my opinion any OOP language should have:

  • Abstract data types and information hiding
  • Inheritance
  • Polymorphism

This can all be done using PHP classes:

<?php
class Something {
    //classes are usually named starting with a cap letter
    var $x;
    function setX($v) {
        // methods start in lowercase then use lowercase to separate
        // words in the method name example getValueOfArea()
        $this-&gt;x=$v;
    }
    function getX() {
    	return $this-&gt;x;
    }
}
?>

Of course you can use your own nomenclature but having a standardized one is useful.

Use coding standards

To develop anything programatically is difficult enough, this is why all programmers must work in an organised and systematic way. It is not obvious but the coding style you employ plays an important role in achieving this goal.

The idea is very simple: consistent code is much easier to maintain.

So, here are several reasons why to use coding specifications:

  • Multi-developer projects require coding standards. Your peer programmers have to understand the code you produce. A coding standard acts as the blueprint for all the team to decipher the code.
  • Even if you develop alone it’s advised to adhere to standards, because it really pays off when you revise your code after a time.
  • Simplicity and clarity achieved by consistent coding saves you from common mistakes.
  • Code becomes extensible and reusable. You don’t have to worry if people working on your current application would understand some reusable feature recycled from the previous project.
  • It just looks professional!

Document your code

So, why do we really need to document our code? Answer to this question may differ from programmer to programmer, but I strongly feel it helps in debugging, reverse engineering, re-engineering, testing and giving the code a neat look. Moreover, it’s a step towards standardization of coding methodology. When you document your code using coding standards, you can then use tools such as phpDocumentor to create professional documentation from your existing php source code.

Use php frameworks

Frameworks provide structure, and speed development. Some popular PHP frameworks such as Zend Framework, CodeIgniter, CakePHP, Symfony, and Kohana follow the Model-View-Controller (MVC) pattern, which itself is a strong advocate of good coding practice. They also usually come with a number of helpful libraries to make your life easier as a web developer.

Re-use code and libraries

Don’t re-invent the wheel. This is the basic concept that should be followed, even though it may seem slightly lazy at times. It actually means that you should not spend much time on solving a problem that has already been solved, in an efficient way, by other programmers. Using already available resources will save you time in your development process.

If you do end up having to create your solution from scratch, give back to the community and share what you’ve accomplished. This is the best part about an open source development community.

Professionally test your code

PHPUnit and SimpleTest are the two most widely used Unit testing suites for PHP. To make code testing viable, good tool support is needed. This is where PHPUnit comes into play. It is a member of the xUnit family of testing frameworks and provides both a framework that makes the writing of tests easy as well as the functionality to easily run the tests and analyse their results. SimpleTest is an open source unit test framework for the PHP programming language and was created by Marcus Baker. The test structure is similar to JUnit/PHPUnit. SimpleTest supports mock objects and can be used to automate the regression testing of web applications with a scriptable HTTP Client that can parse HTML pages and simulate things like clicking on links and submittings forms.

Use an IDE

An IDE provides a one-stop shop for your coding work. An IDE contains an editor in which you can edit the code, debug the code, view your code in a browser (often embedded), and check in and out of source code control. To support that functionality, an IDE has a set of features you don’t find in a basic editor, such as Notepad or Vim. Again, you can extend editors to do a lot of these things, but IDEs have all this functionality in one tidy package and they’re typically pre-configured.

Here are a few well put together and widely used IDE’s.

With so many excellent IDE options out there, some of which are even free, there’s really no reason not to give one a try, particularly if you’re a professional.

  • I use Codelobster PHP Edition.
    It's the best free IDE if taken all round. Besides of standard highlighting and autocomplete for PHP, HTML, CSS and JavaScript it has pair tag highlighting, easy navigation through tags, preview in all browsers, hot keys for marking tags' content, attributes and its value.
    Tooltips for images in code should be also noted, as the ability of navigation through the links with Ctrl-key holding.
    It also includes special HTML code inspector, that allows to compare visible element with the lines of code, that are in charge of it (as in FireBug).
    There also are plug-ins for Drupal, Joomla, WordPress, Smarty and JQuery.

  • I use Codelobster PHP Edition.
    It's the best free IDE if taken all round. Besides of standard highlighting and autocomplete for PHP, HTML, CSS and JavaScript it has pair tag highlighting, easy navigation through tags, preview in all browsers, hot keys for marking tags' content, attributes and its value.
    Tooltips for images in code should be also noted, as the ability of navigation through the links with Ctrl-key holding.
    It also includes special HTML code inspector, that allows to compare visible element with the lines of code, that are in charge of it (as in FireBug).
    There also are plug-ins for Drupal, Joomla, WordPress, Smarty and JQuery.

  • Hi,

    Thanks for this good post dedicated on coding best practices. I agree with most of them, although I don’t use an IDE (unless you consider vim to be one :)) nor do I use a framework… I just wrote a post on the same subject but more focused on the methodology to adopt before coding. I though you might find it an interesting read…
    Cheers,

  • Hi,

    Thanks for this good post dedicated on coding best practices. I agree with most of them, although I don’t use an IDE (unless you consider vim to be one :)) nor do I use a framework… I just wrote a post on the same subject but more focused on the methodology to adopt before coding. I though you might find it an interesting read…
    Cheers,