PHP Singleton Pattern

Posted: 5 Februari 2013 in Creative Zone

<?php

class TukangRoti{
private static $instance ;
private $roti;

public static function dipanggil( ) {
if( !self::$instance) {
self::$instance = new TukangRoti() ;
}

return self::$instance ;
}

public function __construct(){
$this->roti = 10;
}

public function beli(){
$this->roti–;
}

public function tinggal_berapa(){
return $this->roti;
}
}

$tr = TukangRoti::dipanggil();
echo ‘<br> Stok ‘.$tr->tinggal_berapa(); //10
echo ‘<br> Beli ‘.$tr->beli();
echo ‘<br> Stok ‘.$tr->tinggal_berapa(); //9
$tr2 = TukangRoti::dipanggil();
echo ‘<br> Joni Beli ‘.$tr2->beli();
echo ‘<br> Joni Stok ‘.$tr->tinggal_berapa(); //8
?>

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s