2016-04-25 75 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