Welcome to post 12 of my 100 day challenge. Checkout my introduction for some background.
10 PHP development tips part 2
This is the second and concluding part of a two part series I began in April 2013. I never got round to finishing the second part, but here it is in all it’s glory. For the first part of this series click here.
6. Test as you go
Nothing excuses poor testing of a code base. If you test as you code then you are protecting yourself against regressions in the future. I would recommend implementing a unit test framework into your workflow. You can find more about unit testing and more specifically how you can use this with PHP here
An excerpt from the PHPUnits homepage:
PHPUnit is a programmer-oriented testing framework for PHP.
It is an instance of the xUnit architecture for unit testing frameworks.
7. Use a source control / versioning system
I highly recommend using the Git source control system to store and version your code. The advantages as numerous but the 3 most important for me are that I can go back to any revision of my code, I can see in the log who pulled in what changes and I can work in a branch separate from my main repository of code and merge in the changes when it is ready.
There are several cloud services you can use to host your Git repositories such as Github and Bitbucket. Github provides unlimited free public repositories to anyone with an account but you have to pay for private ones. Bitbucket provides a free tier for up to 5 users allowing you to have unlimited free private and public repositories. I would chose Bitbucket if I could go back but I’m too ingrained in the Github ecosystem now. I use Bitbucket as a backup for my main repositories and I highly recommend you do the same.
8. Document your code
Comments are for human consumption, they help subsequent users of your codebase manage your code long after you are gone and they also help you when you come back to it at a later point. Comments should be in the form of “What this code is intended to achieve” rather than what it actually does, as the process may change in the future but the concept and output of your code may stay the same.
There are several software systems available that can run against your codebase and generate documentation for your project. The only caveat to this is that you have to write your comments following a certain format but there are big advantages to doing this. If done properly you can have instant documentation for your project. Helping you when revisiting old code and helping your users use your system.
phpDocumentor is the de-facto API Documentation Generator for PHP since the year 2000.
phpDocumentor 2 is a tool with which it is possible to generate documentation from your PHP source code. With this documentation you can provide your consumers with more information regarding the functionality embedded within your source and not only what is usable to them from your user interface.
9. Don’t be afraid to innovate
Just because it’s not broken doesn’t mean you shouldn’t attempt to fix it. Coupled with unit tests and source control you can pretty much have free reign on any part of your code base.
10. Connect with the PHP community
One of the most powerful aspects of the PHP ecosystem is their community. I have had help and advice from the community many times over the when working on big projects. There are many ways you can get involved:
If you want to contribute by helping to develop php you can by looking how you can get involved here.
Do you have any more PHP tips or tips in general? Can you improve on any of the tips I’ve discussed here? If you can let me know in the comments.