What's happening in our world

Home / Blog / Label: best practices
Label: best practices

Wednesday 11th December 2013 Coping With Legacy PHP Code

Developers, like any creative individuals, tend to like to work on their own projects from start to finish. There's nothing more satisfying than seeing a project go live that you've shepherded the whole way from conceptual planning to development and testing to that sweet, sweet final build. Sadly, the world doesn't always work that way. Whether you're working as part of a larger development team, you've been brought in to redirect a project that went off the rails or you're updating a project that's been around almost as long as PHP has, at some point in your career you're going to run into code that doesn't shine - and you're still going to have to work with it.
At first blush, it can seem pretty overwhelming to pick up a project that's got years of development behind it. As with most actively ongoing PHP projects, the entire codebase has evolved over time as the needs of the client, the user and the technology itself has adjusted over the project's lifespan. Even the language itself has changed dramatically since it was first implemented. Best practices that are commonplace now were barely heard of and rarely used when some projects began, and those that existed at the time were not well known. So what do you do?
The most important thing to do is to examine the codebase in its entirety and decide what's most sorely in need of updating. Prioritising your list of updates can make the job seem less like an impossible mountain to climb and actually more like something that might be completed within your lifetime. What sections of code are you going to be working with most directly? Which aspects are so antiquated that they can barely interface with any new code you write? Are there any gaping security holes? Answering these simple questions can provide you with a roadmap of smaller goals that can quickly be completed, giving you a sense of control over the project.

Don't get caught up in a perfectionist mindset. Is it really important for you to start with small-scale optimisation improvements? That can probably wait until some of the larger issues are sorted out. Above all, though - make sure that you follow current PHP best practices when you're implementing your changes, otherwise you - or another programmer a couple of years down the line - are just going to wind up in exactly the same situation as before.

Posted on December 11th 2013 at 08:11pm

Wednesday 20th November 2013 Top Tips for PHP Beginners

Learning a new programming language can be a huge undertaking, and learning PHP is no exception. Not only do you have to learn the actual syntax and structure, but there are a number of different subtleties unique to any language, and PHP has its fair share. There are some best practices that you should get in the habit of applying to your work, so you don't have to come back later and break yourself of any bad habits. This is by no means a complete list, but it's a good starting point for PHP beginners.
First and foremost, use the latest version of PHP. This might seem like a no-brainer to everyone, but it's surprising how many people are still using outdated versions of PHP. Just make sure that you check with your hosting provider that they're willing to support the version you use. While all hosts should be supporting PHP 5 by now, they may not be supporting the latest subversion (5.5 at the time of writing)
Next, familiarise yourself with the PHP manual. Another one that might seem like a no-brainer, but most programmers learn from a 'Teach Yourself' guide or similar online tutorial system, and some have never touched the PHP manual in their lives. Give it a shot, and you're almost guaranteed to learn something that was left out of your quick-start guide.
If you haven't already switched to one, try out an integrated development environment, or IDE for short. We recently posted about some of the most popular IDEs, but it's important to find one that feels comfortable to you, so test out some of the available options to find your personal favourite. The simplest improvements over a basic text editor can make you wonder how you ever coded without an IDE.
To save yourself a bunch of headaches while you're learning (and after), make sure you have enabled error reporting. It will save you a huge amount of time trying to sort through code that you're not really familiar with, and you'll be instantly thankful for it. Just make sure that you remember to turn it off before you launch, or your benign users will be confused and any malicious users may have a route into your system.
Finally, make sure you get into the habit of writing your PHP code as cleanly as possible from the beginning. Use naming conventions for your variables that actually mean something, because by the time you're finished your project you'll have lost track of the obscure names you gave to your incremental loops. Indent your code and use plenty of whitespace, which will make sorting through things infinitely simpler and faster, and will make it easier for other programmers to help you out if it turns out you need it.

Posted on November 20th 2013 at 07:53pm
Try our free php source code demo
Account Login:

login Forgotten Password?
Connect with us
facebook linkedin twitter rss
© Copyright 2002 - 2018 SourceGuardian Limited
Privacy Policy l Terms & Conditions l Company Info l Contact us l Sitemap l PHP Weekly News