Implement a Singleton Pattern in Javascript

var SingletonClass = (function(){
    function SingletonClass() {
        //do stuff
    var instance;
    return {
        getInstance: function(){
            if (instance == null) {
                instance = new SingletonClass();
                // Hide the constructor so the returned objected can't be new'd...
                instance.constructor = null;
            return instance;

Overloading in PHP

Overloading has a different meaning in PHP. PHP does not support function overloading in the same manner as C++. However it possible to pass different number of arguments to a function using func_get_args.

function findSum() {
$sum = 0;
foreach (func_get_args() as $arg) {
$sum += $arg;
return $sum;

echo findSum(1, 2), '
'; //outputs 3
echo findSum(10, 2, 100), '
'; //outputs 112
echo findSum(10, 22, 0.5, 0.75, 12.50), '
'; //outputs 45.75

Overloading in PHP provides means to dynamically “create” properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.
The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.
Example for property overloading: __set() and __get()
Example for method overloading: __call()


As of PHP 5.4.0, PHP implements a method of code reuse called Traits.

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way which reduces complexity, and avoids the typical problems associated with multiple inheritance and Mixins.

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own.

trait ezcReflectionReturnInfo {
function getReturnType() { /*1*/ }
function getReturnDescription() { /*2*/ }

class ezcReflectionMethod extends ReflectionMethod {
use ezcReflectionReturnInfo;
/* ... */

class ezcReflectionFunction extends ReflectionFunction {
use ezcReflectionReturnInfo;
/* ... */

How does MySQL uses indexes ?

If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to find rows. For example, if you have a three-column index on (col1, col2, col3), you have indexed search capabilities on (col1), (col1, col2), and (col1, col2, col3).

MySQL cannot use an index if the columns do not form a leftmost prefix of the index. Suppose that you have the SELECT statements shown here:

SELECT * FROM tbl_name WHERE col1=val1;
SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;

SELECT * FROM tbl_name WHERE col2=val2;
SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3;
If an index exists on (col1, col2, col3), only the first two queries use the index. The third and fourth queries do involve indexed columns, but (col2) and (col2, col3) are not leftmost prefixes of (col1, col2, col3).

Also usually MySQL just uses 1 index per query. In rare cases it may use multiple indexes for a query. This is called index-merge. Explanation of why MySQL uses just 1 index per query :

Language Comparision


var colors = “green”;

$colors = “green”;


var colors ;
colors = [‘white’,’black’,’brown’];


var colors = Array(‘white’,’black’,’brown’);


var hotel = {
name: ‘Super8’,
rooms: 40,
booked: 25

checkAvailability = function(){
return this.rooms – this.booked;


var hotelName =;
var roomsFree = hotel.checkAvailability();