Singleton pattern


The singleton pattern is used to restrict the creation of only object of a class. This is useful when it is expensive (in terms of resources) to initiate new objects every time the same object is needed. This pattern specifically put this logic of maintaining only one instance inside the class itself.

This pattern can be used in situations like database connectivity, reading and writing to disk, sending emails.

In the example below we use a simple translation class which read a json file with translations. Only one instance is needed since reading file operations are slower. Therefore we can easily use this object as many times as needed for multiple words translation. Since it is a singleton so it will keep only one instance each time we use it in the code.

namespace Patterns;

 * Design pattern - Singleton
 * @author: Hazrat Ali
 * @mail:
 * Date: 2016-07-30
class Singleton
    private static $instance;
    private $dir = 'tr';
    private $language;
    private $translations;

    public function __construct($language)
        $this->language = $language;

    public static function getInstance($language)
        if( ! isset(self::$instance)) {
            self::$instance = new self($language);

        return self::$instance;

    public function translate($text)
        return isset($this->translations[$text]) ? $this->translations[$text] : false;

    private function readTanslationFile($fileName)
        $filePath = $this->getPathAbs($fileName);

        if(file_exists($filePath)) {
            $contents = file_get_contents($filePath);
        } else {
            $contents = json_encode([

            file_put_contents($filePath, $contents);

        $this->translations = json_decode($contents, JSON_FORCE_OBJECT);

    private function getPathAbs($fileName)
        $path = sprintf("%s/%s/%s/%s", dirname(__FILE__), $this->dir, $this->language, $fileName);

        if( ! file_exists(dirname($path))) {
            mkdir(dirname($path), 0777, true);

        return $path;

Yii 2 Query Builder with strange backticks

 $query = (new Query())
            ->select([', itinerary.from_vertex_id, itinerary.to_vertex_id,
    AS from_name, AS to_name, 
             CONCAT(, AS it_name
            ->innerJoin('vertex v1', 'itinerary.from_vertex_id =')
            ->innerJoin('vertex v2', 'itinerary.to_vertex_id =')

Solution: The select method above must always be passed with an array ( [] ), using it with string puts strange backticks around column names.

Install Android Studio 3.0 on Ubuntu 17.10

Use the following steps to install android studio 3.0 on Ubuntu 17.10.

   sudo add-apt-repository ppa:maarten-fonville/android-studio
   sudo apt-get update
   sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 libbz2-1.0:i386
   cd ~/Android/Sdk/emulator/lib64/libstdc++
   sudo mv
   sudo ln -s /usr/lib64/ .

basic sql query

Today we learned how to connect to sql using work bench and command line mysql client. We created a simple table, and inserted a row.

Fixed yoga 910 screen flicker in Ubuntu 17.10

To fix it, create or edit the file at /usr/share/X11/xorg.conf.d/20-intel.conf as root with the following:

Section "Device"
    Identifier "Intel Graphics"
    Driver "intel"
    Option "AccelMethod" "sna"
    Option "TearFree" "true"
    Option "DRI" "3"

And add this:
GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash i915.enable_rc6=0″

to: /etc/default/grub
cmd: sudo update-grub
restart computer.


Ubuntu install PHP mail function

It is not installed by default so you have to install it and then a little configuration is required.

sudo apt install sendmail

The run the following command and choose Y for all the options.

sudo sendmailconfig

Now edit the host file:

sudo nano /etc/hosts

And add or edit as:    localhost localhost.localdomain your_machine_name

To get your_machine_name run:


Raspberry PI enable ssh without a monitor

I spent a dozens of minutes today to learn, how to enable ssh to Raspberry Pi without a monitor. SSH is disable by default after October 2016. Here is a one minute solution.

Attach the sd card to your computer and add an empty file SSH on the root of the sd card partition.