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