本文实例讲述了php实现一个限制实例化次数的类。分享给大家供大家参考,具体如下:
实现思路
实现代码
<?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
如对本文有疑问, 点击进行留言回复!!
php 使用 yansongda/pay 进行微信,支付宝支付
Codeforces Round #658 (Div. 2) (C1、C2)
van-uploader + thinkphp6.0 图片上传
网友评论