?php
class demo{
public $name;
public static $count=0;
private function __construct($name){
echo "create $name br/>";
$this->name = $name;
self::$count++;
}
public function __destruct(){
echo "destory ".$this->name."br/>";
self::$count--;
}
public static function create($name){
if(self::$count>2){
die("you can only create at most 2 objects.");
}else{
return new self($name);
}
}
}
$one = demo::create("one");
$two = demo::create("two");
$two = null;
$three = demo::create("three");
运行结果:
create one
create two
destory two
create three
destory three
destory one