Rajiv
Rajiv

Reputation: 31

How can I restrict user to only created limited numbers of objects

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

How can I restrict user to only created limited numbers of objects. For above example if I will create one more object then I will throw an error.

Upvotes: 1

Views: 97

Answers (2)

kay27
kay27

Reputation: 909

Add static counter variable to your class, add constructor and destructor to increase and decrease it. Check the value in constructor:

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

Script output:

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

Upvotes: 2

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

The solution is:

  • Keep the constructor method of the Book class private.
  • Declare two class members named $number_of_objects and $threshold_number_of_objects.
  • Use a separate class method, say getInstance() method to create new instances of Book class. This getInstance() method will create new instances of class Book if the number of objects already created is less than the threshold value, otherwise it'll return error.

So your code should be like this:

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 />";    
}

Output:

English
Science
Error: Number of objects crossed the threshold value 

Upvotes: 1

Related Questions