PHP : DB Connection

<?php

/**
* Handling database connection through PDO
*/
class DbConnect {

/**
*Class is use for to create the connection, also here we have assign tables name
*/

public $conn = null;

/**
*Tables Name
*/

public $tablePrifix ;

function __construct() {
$this->conn =  $this->connect();
$this->tablePrifix = TABLE_PREFIX;
}

/**
* Create a connection with database using PDO
* @return type
*/
function connect() {
if ($this->conn == null) {
try {
$this->conn = new PDO(‘mysql:dbname=’ . DB_NAME . ‘;port=’ . DB_PORT . ‘;host=’ . DB_HOST, DB_USERNAME, DB_PASSWORD);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$this->conn->exec(“set names utf8”);
} catch (PDOException $e) {
echo $e->getMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
}
// returing connection resource
return $this->conn;
}

}

$db = new DbConnect();
$select = “SELECT count(0) FROM {$db->tablePrifix}{$db->deviceToken}”;
$where = ” WHERE device_id = :device_id AND type = :type”;
$query = $db->conn->prepare($select . $where);
$query->bindParam(‘:device_id’, $this->deviceId, PDO::PARAM_STR);
$query->bindParam(‘:type’, $this->deviceType, PDO::PARAM_STR);
$query->execute();
return $query->fetchColumn();

ZF2 : Finish Event

public function onBootstrap(Event $e)
{
    $app = $e->getTarget();
    $app->getEventManager()->attach('finish', array($this, 'doSomething'), 100);
}


public function doSomething ($e)
{
    $response = $e->getResponse();
    $content = $response->getBody();
    // do stuff here
    $response->setContent($content);

}

Javascript : Sprintf Function

var sprintf = function(str) {
var args = arguments,
flag = true,
i = 1;
str = str.replace(/%s/g, function() {
var arg = args[i++];
if (typeof arg === ‘undefined’) {
flag = false;
return ”;
}
return arg;
});
return flag ? str : ”;
};