On some opensource codes, why do they make use of:
$router = Cfw_Router::getInstance();
Instead of just using
$rounter = new Cfw_Router();
Does it have advantages?
getInstance():
public static function getInstance() {
if (null === self::$__instance) {
self::$__instance = new self();
}
return self::$__instance;
}
They’re making use of the singleton pattern. Basically, it allows only one instance to exist at any given time. However, it can sometimes be used to provide a global variable, which is not always the best thing in a design (see the criticisms in the link above).