2016-04-25 71 views
1
<?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(); 
?> 

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

+0

另一种方法是创建一个单独的类,这样可以确保始终有类的1个实例从不2 。 – Daan

+0

你的情况应该是什么限制? 2个对象? – RomanPerekhrest

+0

在我的情况下,它不应该超过2。 – Rajiv

回答

2

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

<?php 
class Book 
{ 

    var $name; 
    private static $counter=0; 

    function __construct() 
    { 
    self::$counter++; 
    if(self::$counter > 2) 
     throw new Exception('Limit exceeded'); 
    } 

    function __destruct() 
    { 
    self::$counter--; 
    } 

    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(); 

$objectthird = new Book; 
$objectthird->setName('Test'); 
echo $objectthird->getName(); 

脚本输出:

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 
+1

将静态属性'$ counter'标记为私有,以避免外部修改 – RomanPerekhrest

+0

@RomanPerekhrest thx,done – kay27

+1

我会更改异常消息文本,但是,无论如何...你有我的upvote(+10) – RomanPerekhrest

1

解决的办法是:

  • 保持privateBook类的构造方法。
  • 声明名为$number_of_objects$threshold_number_of_objects的两个类成员。
  • 使用单独的类方法,说getInstance()方法来创建Book类的新实例。如果已创建的对象数量小于阈值,则此getInstance()方法将创建类Book的新实例,否则它将返回错误。

所以,你的代码应该是这样的:

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