The Six rules to be a perfect PHP 5 switcher
It’s so strange to hear about PHP 4, while PHP 5.3 is out there. Probably because there are a lot of frameworks and cms having a PHP 4 fallback.
The first steps you need to know to be a perfect PHP5 switcher, are easy:
- Be OOP
It can be useful read some books, like the most important one Thinking in Java, which is free. Java apart, It explains you the importance to build your library and your works in blocks. You always should try to create your code in small pieces and reusable objects, and use classes and methods instead functions, if you can.
- Be DRY
Every time you write your code, spend a little time to discover if it can be reusable and what you need to do that.
- Be organized
You MUST know about MVC (Model/View/Controller) patterns and Design Patterns. Putting code in the right place it’s more simply than you think
If you are acting on db, you are in Model pattern
If you are handling user input or you are using fetched data, you are in the Controller pattern
If you are writing the interface, you are in the View pattern
- Don’t feel alone
Think every time you write your code someone could be working with it:
- use explanatory names for your methods, classes, functions, variables, constants (ex. do_coffee(), $total_smiles, LIGHT_SPEED) and avoid using numeric variables (ex: $sql1, $sql2…)
- smart comments on your code. Write the proposal and avoid a simply code translation
- learn PHPDoc
- Be version controlled
What happens if you lose your code or some changes? Don’t be fool, use Mercurial or Git. Subversion at least. You can have your free repository, if your server hasn’t one.
- Be unique but not stupid
Before writing your code, put down a schema and try to not write your code from scratch. Use frameworks or reusable libraries and help their communities, if you can. Take a look to : Symfony, Zend Framework, Pear