Modern PHP Development The Right Way


PHP

PHP is one of the few server-side scripting languages that have evolved rapidly on account of being the preferred platform for web development. Unlike previous versions, It now contains all the components for being an absolutely robust language be it flexibility or dynamic architecture. Interestingly the latest PHP version completely supports the OOP concept. A bunch of best practices for development of modern PHP applications will really help you leveraging this popular platform for web application development. On behalf of my experience, I would recommend following practices to be included in your PHP development.

 

1.Password hashing

 Unfortunately, a majority of users are neither serious nor aware about the importance of strong passwords. In case the database is compromised then user privacy will be at risk but using password hashing you can resolve the security concern. With the recent leap in technical advances md5() is more a thing of past as hackers are now capable of millions of combinations per second to crack the password on a single GPU. hashing algorithm will make it almost impossible for hackers to crack user password and, you will still be able to match the generated hash with the original password in future. Now PHPass will be an excellent option to implement password hashing. Let see how to do about it.

 

Step 1- Install PHPass. Get PHPass here.

Step 2- Include the file(PHPass)

Step 3- Initiate the class

Step 4- Set Password Length

Step 5- Set the Password

Step 6- Hash the password

Step 7- Collect the hash from browser( to be stored in Database)

Step 8- Use CheckPassword method (to ensure hash matches an unhashed password)

 

Below image will help you to have a clear understanding about the usage of Password hashing.

PHP password hashing.png

Image Credit: phpacademy

 

2. Email Validation

The validation of email hold utter importance as far as user contact records are concerned. It is quite simple task that sometimes is considered difficult. Consider the below code snippet for this. It is a clean and faster way to ensure email validation.

PHP password hashing2.png

Image Credit: phpacademy

 

3. Use Namespace

PHP script doesn’t allow two or more classes with exactly same name. If you are developing a big project where you require connectivity between two databases in a single project and there are multiple clasess with same name then using namespace you can limit rather eliminate any ambiguity completely hence, avoiding any unwanted class name collision while calling them. It reflects smart programming skills that demonstrates programmer has good command over OOPS concepts.

 

A typical code snippet for Namespace implementation.

php namespace.png

Image Credit: tutorialspoint

 

4. Class Autoloading

Defining each PHP class inside separate file is recommended the coding practice. A class defined in a separate class is first required before it can be used for object creation. When there are multiple separate classes it will end up in huge number of class requiring in each script thereby, messing up the code maintenance. This can be smartly avoided by configuring PHP with the addition of autoload  function to automatically load class files on demand.

 

Below image reveals how autoload function  can avoid error when non existing class is called.

php autoloder diagram.png

Image Credit: daveh

 

Autoload code snippet

PHP autoload code.png

Image Credit: daveh

 

Looking forward to responding to your queries and comments regarding right approach towards modern web development using PHP. If any information you want to get included I will update this article with it. In the meanwhile, I would also suggest you not sticking to usage of short tags and leave an impression of unprofessional outlook. Remember PHP interpreter will consider all unquoted strings inside the array index will be considered as constant and it could incorrectly auto-define them. This will not have a sudden impact but can delay the execution.

 

About Singsys Pte. Ltd. Singsys is a solution provider that offer user friendly solution on cutting edge technologies to engage customers and boost your brand online results from a set of certified developers, designers who prefer optimized utilization of the available resources to align client’s idea with their skillset to reflect it into a Mobile application, Web application or an E-commerce solution

 

You may be interested in following:

  1. Importance of Prevent SQL Injection in PHP
  2. Twitter Streaming API in PHP Complete Guide
  3. PHP Image Manipulation Step by Step and Easy
  4. PHP, PYTHON or RUBY an in depth comparison
  5. PHP Arrays and Effective Way of Working