15 EKİM 2008, ÇARŞAMBA
PHP5 içinde Singleton tasarım deseni oluşturma
Nasıl bir Tekil sınıf PHP5 sınıfları oluşturmak istiyorsunuz?
CEVAP
15 EKİM 2008, ÇARŞAMBA
/**
* Singleton class
*
*/
final class UserFactory
{
/**
* Call this method to get singleton
*
* @return UserFactory
*/
public static function Instance()
{
static $inst = null;
if ($inst === null) {
$inst = new UserFactory();
}
return $inst;
}
/**
* Private ctor so nobody else can instance it
*
*/
private function __construct()
{
}
}
Kullanmak için:
$fact = UserFactory::Instance();
$fact2 = UserFactory::Instance();
$aslında == $fact2;
Ama:
$fact = new UserFactory()
Bir hata atar.
http://php.net/manual/en/language.variables.scope.php#language.variables.scope.static statik değişken kapsamları ve static $inst = null;
ayarı neden ve nasıl çalıştığını anlamak için.
Bunu Paylaş:
Singleton tasarım deseni vs Singleton ...
SQLiteDatabase için kullanarak Singlet...
C Singleton tasarım deseni...
Belgeler içinde bir klasör iOS apps kl...
Python oluşturma singleton...