Write Modern PHP

Submitted by Behrad Khodayar on Mon, 05/29/2017 - 11:58

PHP is one of the easiest scripting languages (turning into a full programming language) out there. Generally speaking, being an easy-to-use stuff, could be considered as an advantage for everything; but due to the trade-off principle, gaining the quality of simplicity, comes in return for losing another quality[ that is High Quality Code in our case ].

Working with tens of developers during my years of experience, has proven (at least for me) that 70% of PHP developers write:

  • Low quality
  • Dirty & disordered
  • Neither OO nor functional
  • Pattern-less
  • Non-unique front controller

code. But those developers are not the only blameworthy guy at producing such a mess! PHP itself, despite majority of programming languages, DOES NOT FORCE DEVELOPERS TO WRITE UNIFORMED & HIGH QUALITY CODE.

Chained headaches appears when your software starts to grow & scale up. Its when any minor try to extend or while maintaining the code becomes impossible. As a more tangible example, assume a web application which you can access its different pages by simply browsing their exact file name(something.php). Now in order to make your app more secure, you have to visit all pages one by one, because you have plenty of front gates to the outer world .All of files can be accessed directly through web, thus attacker is welcome to all of them(e.g. by passing parameters!). One of the best practices to avoid this situation is to create a front-controller(different from controller concept in MVC), and restrict external access to the whole app to one & only one file. This way you just need to parse, purify & filter harming parameters in one section & you can safely continue to tweaking other parts.

There are many guidelines one can follow or best practices to implement to produce a high quality code in PHP. I'm gonna mention some of those here, just in Title. For now, Anyone interested in topics, can google about them & learn how to achieve that particular standard or quality. I will write detailed articles explaining each item in depth & suggesting practical implementation solution[Use #Modern PHP tag to find them]:

[Items here aren't listed based on importance or any other criterion, Notice I've used "unordered list" for it!]

  • Single Front Controller
  • Load files using Autoloading
  • Dependencies with Composer
  • Separation of Concerns
  • Choose OO over structured
  • Functional PHP in its right way & use case
  • Design Patterns
  • DRY, Rule of three, ...
  • ...
p.s. This list may be completed in future.

Add new comment