1

I have a class which does some API (REST) functions. I'd like to declare the variable class-wide variable and static, so I won't fetch the data over and over again. (This is what I think in theory, could be wrong) Many methods in the class will need that data.

I am using this type of class but something doesn't look right. It is working, but (this is just an example class, not the real code);

class Some_Process {

    private static $tickets = array();

    private function _get_tickets () {
        if(!self::$tickets) {
           $curl = curl_init();
           curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
           curl_setopt($curl, CURLOPT_URL, 'http://someurl');

           self::$tickets = json_decode(curl_exec($curl));
           if(!self::$tickets) {
               return FALSE;
           }
           return TRUE;
        }
    }
    function process_tickets () {
       self::_get_tickets();
       //here I start using the varible
       do some job on .. self::$tickets;
    }   
}
2
  • 1
    It's nice enough :) I would return self::$tickets and not a boolean, but it's maybe just a matter of taste. Commented Aug 9, 2011 at 9:52
  • Thanks :) Actually my function is setting number of variables (i have stripped it down). So that's why I was returning boolean. Commented Aug 9, 2011 at 10:00

2 Answers 2

1

There is one problem, what if the actual number of tickets returned by http://someurl is zero? then if(!self::$tickets) will always be true even if we have already filled the array. I would initialize $tickets with null:

private static $tickets = null;

and change that condition to:

if( self::$tickets === null ) {
    self::$tickets = array();
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

And singleton pattern ?

class Some_Process {
   protected static $_instance;
   private $_tickets = array();

   public static function initialiaze() {
       $curl = curl_init();
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
       curl_setopt($curl, CURLOPT_URL, 'http://someurl');

       self::getInstance(json_decode(curl_exec($curl));
   }

   public static function getInstance($tickets = null) {
       if (!self::$_instance) {
            self::$_instance = new self($tickets);
       }

       return self::$_instance;
   }

   private function __constructor($tickets) {
       $this->_$tickets = $tickets;
   }

   public function getTickets() {
       return $this->_tickets;
   }

   public function processTickets() {
       $this->_tickets();
       ...
   }
}

with Singleton pattern you instantiate your object one time so you crawl tickets one time.

eq:

Some_Process::initialize();
Some_Process::getInstance()->processTickets();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.