mercredi 6 mai 2015

PHP :: Storing a database connection v. static instantiation

Which of these approaches is more efficient (or proficient) in PHP?

class Amazing {
    protected $db;

    public function __construct () {
         $this->db = new Database::getInstance();
    }

    public function do ($blah) {
         $this->db->doInsert($blah);
    }

    public function again ($blah) {
         $this->db->doUpdate($blah);
    }
}

OR:

class Less_Amazing {

    public function do ($blah) {
         $db = new Database::getInstance();
         $db->doInsert($blah)
    }

    public function again ($blah) {
         $db = new Database::getInstance();
         $db->doUpdate($blah)
    }
}

A pint of beer is riding on the answer.

Aucun commentaire:

Enregistrer un commentaire