Consider a list of nice things way too many developers are overlooking in PHP projects. Knowing those will allow you to keep your cool hats on for like 24/7 hence you will actually rock at what you are doing. Technically there will be nothing hard or revolutionary in the tips I will be listing below. Though they are often overlooked which, in turn leads to various complications that could have been avoided. So, here we go, don’t take the cool hats off!
- Declare is an unusual statement in PHP. For now ticks (functions which are executed only after all n-th statement/function) are the only thing supported by declare. The impact these ticks may have on both testing and timing are hardly explainable with puny words. Simply imagine the possibilities that will be open to you after you have the power to chart the length of specific processes as they are happening without any necessity of manually placing those timing functions all over the code. You are also able to pass a state to your tick function for some analysis. I call it killing two birds with one stone hence: a) you get a chance to see how data changes through various within-app processes and b) it’s great for tracking those pesky bugs.
- Try using abstract classes as well as interfaces when classes are following precise specific contracts. Interfaces will define all require function arguments and functions for every implementing class. Abstract classes are something of the same sort though they are doing one nice thing as well: they allow functions to be defined regularly. Abstraction layers will be the perfect place to define your single interface in one place with assistance from abstract classes + interfaces allowing the extending classes to manage data-specific logic implementation. Pro tip: Interfaces require implementation when abstract classes require extension rather than direct instantiation.
- There is a difference between “===” and “==”! When the first option’s looking for equality the second one goes for identity. Meaning 1 may be equal to ‘1’ though it will not be identical. Consider using this knowledge wherever 0 isn’t quite equivalent to FALSE.
- Try using spl_autoload() instead of _autoload(). Why bother? Well, _autoload is called by PHP whenever any non-existing class is being installed. Then it will look up the file class is in and will include it. Afterwards the program runs its usual course. Though this is quita a handy thing direct implementation may not be the best choice. There is no namespace meaning two or more applications will try to crash or display an error if run at the same time and some files are the same.
This is not all, keep looking for part 2 that will be published shortly.