anatoo.jp twitter

PHPでのシングルトンパターン

まあ自分の場合はこう書くかなあ

<?php

class Klass
{
    private function __construct() { } 
    static function getInstance()
    {
        static $obj = null;
        return $obj ? $obj : $obj = new self; // 5.3以降なら return $obj ?: $obj = new self;
    }
}

Klass::getInstance();

ネタ元: http://blog.asial.co.jp/483