Singleton pattern

Singleton pattern

is a creational pattern – one of the three basic design pattern.

As the name describes, it’s something which deals with a single value or it works as a unique thing.

In programming, it’s a class which have only one object in the entire execution process.

According to a programming language, you’ll have to set up the class in such a way that only one object will be created in the entire process.


  • Basic programming knowledge.
  • A basic oops concept like what is class, method, property, etc.
  • More understandable if you know PHP

In this, I’m going to use PHP as a programming language to implement the singleton pattern.

  • Make constructor – __contruct method – private.
  • Make __clone method private or keep it public, but let you always return $this variable.
  • Define a public static method to create an object of the same class.
  • Define a private static variable to check if the object created or not. Or keep the created object in that variable and return the same value if requested – I’m going to return the same object created in the previous call. I’ll create one if it’s called first time, and I’ll store it in the static variable.


  1. <?php
  3. /**
  4.  * Description of SingletonPattern
  5.  *
  6.  * @author Ahmad Asjad <>
  7.  */
  8. class SingletonPattern {
  10. public $param1;
  11. public $param2;
  12. private static $selfInstance;
  14. private function __construct($param1, $param2) {
  15. $this->param1 = $param1;
  16. $this->param2 = $param2;
  17. }
  19. public function __clone() {
  20. return $this;
  21. }
  23. /**
  24.   * @param string $param1
  25.   * @param string $param2
  26.   * @return SingletonPattern
  27.   */
  28. public static function getInstance($param1, $param2) {
  29. if (empty(self::$selfInstance)) {
  30. //Pass params to be passed to the constructor
  31. self::$selfInstance = new static($param1, $param2);
  32. }
  33. return self::$selfInstance;
  34. }
  36. }



Run xdebug with quick setting

Many people have problems in enabling the xdebug module to debug their code. Let’s see how easy it’s to run xdebug with quick setting.

Steps to run xdebug

Step 1

Install xdebug if not already installed. after installation check whether it’s installed correctly or not (using phpinfo();)

Step 2

Edit php.ini and paste the sample of code:

If you are using Ubuntu, your php.ini path is something like /etc/php/7.1/apache2/php.ini

If you are using the console application of PHP, the path will differ: /etc/php/7.1/cli/php.ini

  1. ; Added for xdebug
  2. zend_extension="/usr/lib/php/20160303/"
  3. xdebug.remote_enable=1
  4. xdebug.remote_autostart = 1
  5. xdebug.remote_handler=dbgp
  6. xdebug.remote_mode=req
  7. xdebug.remote_host=localhost
  8. xdebug.remote_port=9000
  9. xdebug.profile_enable=1

change the above code according to your requirement

Step 3

Restart apache, if you want to use it instantly.

Now tell your editor to listen for xdebug.

How to enable PHP mail function on Ubuntu

Here are steps to follow:

First of all install packagephp-pear if it’s already not installed. apt-get install php-pear
Then install following PEAR packages

  • pear install mail
  • pear install Net_SMTP
  • pear install Auth_SASL
  • pear install mail_mime

Then install POSTFIX

apt-get install postfix

This command will bring dialogue step by step to setup mail server.

Select Internet Site

and choose domain mail to be sent from

Try to send mail using mail() function, and you’ll receive the one you sent.

Anonymous function example in PHP


  1. <?php
  3. /**
  4. * An array function to work with its individual value whatever we want.
  5. * @param array $array
  6. * @param function $function
  7. */
  8. function anonymous_test($array, $function){
  9. foreach ($array as $single){
  10. $function($single);
  11. }
  12. }
  13. ?>



  1. <?php
  2. /**
  3. * Here we are writing a function which will print only if it's printable.
  4. */
  5. $arr = [1,2,3,['A','B'],4,5];
  6. anonymous_test($arr, function($value){
  7. if(!is_array($value) && !is_object($value)){
  8. echo $value;
  9. }
  10. });