Php 如何限制用户只创建有限数量的对象

Php 如何限制用户只创建有限数量的对象,php,Php,如何限制用户只创建有限数量的对象。对于上面的例子,如果我要再创建一个对象,那么我将抛出一个错误 将静态计数器变量添加到类中,添加构造函数和析构函数以增加和减少它。检查构造函数中的值: <?php class Book { var $name; function setName($name){ $this->name = $name; } function getName(){ return $this->name ;

如何限制用户只创建有限数量的对象。对于上面的例子,如果我要再创建一个对象,那么我将抛出一个错误

将静态计数器变量添加到类中,添加构造函数和析构函数以增加和减少它。检查构造函数中的值:

<?php
class Book
{

var $name;
   function setName($name){
          $this->name = $name;
   }
   function getName(){
         return $this->name ;
   }
}
$objectfirst = new Book;
$objectfirst->setName('English');
echo $objectfirst->getName();
$objectsecond = new Book;
$objectsecond->setName('Science');
echo $objectsecond->getName();
?>
解决办法是:

  • 书籍
    类的构造函数方法保持为私有
  • 声明两个名为
    $number\u of_objects
    $threshold\u number\u of_objects
    的类成员
  • 使用单独的类方法,例如
    getInstance()
    method来创建
    Book
    类的新实例。如果已创建的对象数小于阈值,则此
    getInstance()
    方法将创建类
    Book
    的新实例,否则将返回错误
因此,您的代码应该如下所示:

EnglishScience
Fatal error: Uncaught exception 'Exception' with message 'Limit exceeded' in sandbox/scriptname.php:12
Stack trace:
#0 sandbox/scriptname.php(36): Book->__construct()
#1 {main}
  thrown in sandbox/scriptname.php on line 12

另一种方法是创建一个singleton类,这确保该类始终有1个实例,而不是2个实例。在您的情况下,限制应该是什么?2个对象?在我的例子中不应该超过2个。@Rajiv下面给了你两个答案。选择适合您的答案,并将其标记为已接受。将static属性
$counter
标记为private以避免外部修改我会更改异常消息文本,但是,无论如何……您得到了我的投票(+10)
EnglishScience
Fatal error: Uncaught exception 'Exception' with message 'Limit exceeded' in sandbox/scriptname.php:12
Stack trace:
#0 sandbox/scriptname.php(36): Book->__construct()
#1 {main}
  thrown in sandbox/scriptname.php on line 12
class Book{

    private $name;
    private static $number_of_objects = 0;
    private static $threshold_number_of_objects = 2;

    // Since it's constructor method is private
    // it prevents objects from being created from 
    // outside of the class
    private function __construct(){}

    // It creates an object if the number of objects
    // already created is less than the threshold value
    public static function getInstance() {
        if(self::$number_of_objects < self::$threshold_number_of_objects){
            ++self::$number_of_objects;
            return new Book();
        }else{
            echo "Error: Number of objects crossed the threshold value";
            return false;
        }
    }

    public function setName($name){
          $this->name = $name;
    }

    public function getName(){
         return $this->name ;
    }
}

//$objectfirst = new Book;  invalid
$objectfirst = Book::getInstance();
if($objectfirst){
    $objectfirst->setName('English');
    echo $objectfirst->getName() . "<br />";    
}

//$objectsecond = new Book;  invalid
$objectsecond = Book::getInstance();
if($objectsecond){
    $objectsecond->setName('Science');
    echo $objectsecond->getName() . "<br />";
}

//$objectthird = new Book;  invalid
$objectthird = Book::getInstance();
if($objectthird){
    $objectthird->setName('Engineering');
    echo $objectthird->getName() . "<br />";    
}
English
Science
Error: Number of objects crossed the threshold value