Using Static Methods and Properties to Limit Instances of a Class (PHP 5 Only) : static properties « Class « PHP






Using Static Methods and Properties to Limit Instances of a Class (PHP 5 Only)

 
<?php

class Shop {
  private static $instance;
  public $name="shop";

  private function ___construct() {
  }

  public static function getInstance() {
    if ( empty( self::$instance ) ) {
    self::$instance = new Shop();
    }
    return self::$instance;
  }
}

$first = Shop::getInstance();
$first-> name="A";

$second = Shop::getInstance();
print $second -> name;
?>
  
  








Related examples in the same category

1.static class properties
2.static class members
3.Using the -> and :: operators to call hypnotize
4.Use the static modifier to change a member or method