@raheelmasood

PHP Developer

My PHP Story – 20 Years of PHP — August 27, 2015

My PHP Story – 20 Years of PHP

PHP – People Helping People

Following the twitter trend of #20yearsofphp, I could not resist to share my experience of my little PHP career šŸ™‚

I am a commerce graduate and spend almost 5 years of my professional career in areas like accounts, customer service, BPO. I learned HTML during my school days in early 2000. But I never thought that this web world would become the most challenging and career oriented field.

After struggling through the ups and downs of random jobs, I, with the continuous motivation from some of my friends, finally jumped into PHP in Feb 2014.

At the start it was a bit tough for me as IĀ had no programming background. But I must say that when I look around other languages, If IĀ picked up any other language for start, I might not be able to continue.

As of today, I have developed several projects with variety of CMS and Frameworks offered by some of the fantastic minds.

My love for PHP is growing day by day and in near future IĀ have no plans to switch into any other language.

What PHP gave me:

  • A better step towards future
  • No threat of being jobless
  • Chances to explore international market
  • Side income with freelancing.
  • Satisfaction with what I am now

What I gave to PHP:

I tried many times to create some composer packages. But ended up doing nothing :).Ā I will keep trying unless IĀ found my self capable to contribute in open source projects.

If you are a PHP Lover, show your love by creating a blog post about your journey and put that it on twitter with #20yearsofphp

Multiple Database Instance With single class — June 25, 2015

Multiple Database Instance With single class

During the development of an API for a client i feel the need to have one class that can return me instance of different databases. I made a singleton pattern to achieve this. Below is the class that you can utilize if needed.

<?php

class Connection {

	/**
	 * [$_mongo_connection description]
	 * @var null
	 */
	private static $_mongo_connection = null;

	/**
	 * [$_mysql_connection description]
	 * @var null
	 */
	private static $_mysql_connection = null;

	/**
	 * A private construction to achieve singleton
	 * @param string $type type of database
	 */
	private function __construct($type)
	{
		switch ($type) 
		{
			case 'Mongo':
				self::$_mongo_connection = new MongoClient('mongodb://localhost');
				break;

			case 'MySQL':
				$dsn = 'mysql:dbname=mydatabase;host=localhost';
				$user = 'root';
				$password = 'mypassword';	
				self::$_mysql_connection  = new PDO($dsn, $user, $password);
				break;

			default:
				return;
				break;
		}
	}

	/**
	 * Returns instance of MongoDB
	 * @return  Resource
	 */
	public static function getMongoInstance()
	{
		if(is_null(self::$_mongo_connection))
		{
			new Connection('Mongo');
		}
		return self::$_mongo_connection;
	}

	/**
	 * Returns instance of MongoDB
	 * @return  Resource
	 */
	public static function getMySQLInstance()
	{
		if(is_null(self::$_mysql_connection))
		{
			new Connection('MySQL');
		}
		return self::$_mysql_connection;	
	}

	/**
	 * Set the instannces to null on object disposal
	 */
	public function __destruct()
	{
		self::$_mongo_connection = null;
		self::$_mysql_connection = null;
	}
}
What is message queue in programming — June 18, 2015

What is message queue in programming

If you have been programming in any server side language, I believe you have came up with the scenario where you make stuffs like sending bulk emails, processing some images, video conversions and other heavy lifting tasks.

If you are not aware of what message queue is and how you can use it in your day to day programming. This tutorial will give you a summarize introduction of what message queue is and how you can utilize it.

What is message queue

  1. Message Queuing (MSMQ) technology enables applications running at different times to communicate across heterogeneous networks and systems that may be temporarily offline. Applications send messages toĀ queues and read messages from queues. – Source : MSN

What this mean is that a message queue is a mechanism where process communicates with each other. These processes can be within a same application or they can be on different application and even on a different server that are connected in network.

Typically, message queue consists of 3 entities.

  1. Producer
  2. Server (Message Box)
  3. Consumer

Producer

A producer is someone who has the message that needs to be send to the Message Box.

Server (Message Box)

A storage area where messages are stored in queue typically in FIFO (First In First Out) method. This message box continuously pushes messages to the Consumer.

Consumer

A consumer is a process that process the message.Ā 

Example Workflow

For the purpose of simplicity lets take a real world example where you have to take out email address from a CSV file and you have to send them an email. If there tens of email address you definitely do not need a queue but what if there are millions of records? Are you going to make your user wait for long time until the process finished? You will not.

Generally you will make a producer script that fetches CSV data and pass it to the Message Box. At this stage you let your user go and relax and show him a message ‘Your request received’. On the other hand you will have consumer script that listens to the Message box and process the data that may arrived.

What if connection lost or any exception occurs ?

This is where a message queue shines. Unlike cronjobs, A message queue is reliable in a sense that there is almost no chance of loosing the data in case of any unwanted thing happens in the server.

This becomes possible because of the acknowledgement system. A message queue holds a message in box as long as it does not receive acknowledgement from consumer like ‘Hey, I have processed this message. You can dispose this message now!’.

Resources

While PHP offers supports for message queue by itsĀ Semaphore Functions. You are not likely going to implement in real world message queue because of its lacking in multi servers supports.

There are other third party resources that helps you build a message box quickly and leave you only to worry about the processing of message.

  1. Gearman
  2. RabbitMQ