new static and new self

http://stackoverflow.com/questions/5197300/new-self-vs-new-static

What is the difference between new self and new static?

self refers to the same class in which the new keyword is actually written.

static, in PHP 5.3’s late static bindings, refers to whatever class in the hierarchy you called the method on.

In the following example, B inherits both methods from A. The self invocation is bound to Abecause it’s defined in A‘s implementation of the first method, whereas static is bound to the called class (also see get_called_class()).

class A {
    public static function get_self() {
        return new self();
    }

    public static function get_static() {
        return new static();
    }
}

class B extends A {}

echo get_class(B::get_self());  // A
echo get_class(B::get_static()); // B
echo get_class(A::get_static()); // A
Advertisements

when to use private constructor

constructor must be declared as public if you want to use ‘new ClassName()’.

Sometimes, you want constructor to be private so it won’t be called outside of the class. That is when you are using singleton.

http://stackoverflow.com/questions/12553142/when-we-should-make-the-constructor-private-why-php

hh

  1. Constructor Argument Promotion —- The class members will automatically be created and assigned to what is passed into the constructor.
  2. lambad function using ==>  e.g., $squared = array_map($x ==> $x*$x, array(1,2,3));
opt
äpt/
verb
  1. make a choice from a range of possibilities.