This article is about Manageable,Clean coding in PHP.
PHP is one of the most popular Web Development programming language. PHP code in requested file is executed by server, to create dynamic web page content. When I started to develop web sites in PHP, I just wanted things to work in web sites,No matter how I did it. My intention was to complete the task according to the user’s requirement, That’s all most of the beginners want initially,unless someone guide them. There is no problem in that. But you must start to develop yourself, each of your project must be more flexible,manageable than previous one.
Developers thinking depends on how they implement the solution to the specific problem.
For example, consider simple system for student register. Most of the beginner will just start the development on the go. Due to lack of experience or lack of good guidance, even the complete system will not be the flexible, Because of unplanned,messy code. Most of the User’s don’t accept the first build,they will definitely modify their requirement and the system will get more and more complicated. And due to unman-aged,unplanned code frustration rises, So thought about writing this article for those beginners who find it frustrating to develop large scale projects with unmanageable,unplanned code.
So I’m going to provide way to develop simple student information project in PHP in Clean way.
“No one is perfect in this world”, I’m no exception to this. This is my approach, some of you may still find it unmanageable,please let me know about it.
Step 1: DATABASE – Obviously we need storage for saving/retrieving the data. For our project let’s create simple table to store student information.
Step 2:Database Connection I use PDO connection for database connection. It is best to wrap the PDO in class for flexible usability.I use the following class for PDO (source:Stack Overflow).
Step 3:Entity Class Create php class for student entity Student.class.php.
Step 4: Create php class for business logic on student object StudentLogic.class.php.
Step 5: Using the class services Now we have all the infrastructure ready, we just need them to use as per requirement.
I. Initialize the objects
II. Add Student.
III. Retrieving All Students
Now most of you have got idea about clean code in PHP. For any queries/suggestions please comment below.