netpumber Posted December 10, 2013 Share Posted December 10, 2013 Hello guys. I have a file with the above function that returns the $db variable. function mysql_init(){ $db = new PDO('mysql:host=localhost;dbname=geneticDb;charset=utf8', 'dev', 'dev'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $db; } Now i want to use this variable in every method of the above class without call again that function in each method. Somehow i want to call this once and then just use the $db variable. <?php require_once('mysql.php'); class Note { private $_done; private $_noteType; private $_plantId; private $_noteTitle; private $_noteText; private $_regenerationDate; private $_noteId; public function __construct() { } public function addNote($noteType, $plantId , $noteTitle, $noteText) { $this->_noteType = $noteType; $this->_plantId = $plantId; $this->_noteTitle = $noteTitle; $this->_noteText = $noteText; $db = mysql_init(); $sql = "INSERT INTO notes ( noteId , noteType, plantId, noteTitle, noteText ) VALUES ('', ?, ?, ?, ?)"; $prq = $db->prepare($sql); $prq->bindValue(1,$this->_noteType, PDO::PARAM_STR); $prq->bindValue(2,$this->_plantId,PDO::PARAM_INT); $prq->bindValue(3,$this->_noteTitle,PDO::PARAM_STR); $prq->bindValue(4,$this->_noteText,PDO::PARAM_STR); ($prq->execute()) ? $this->_done = 1 : $this->_done = 0; if ($this->_done == '0') { $json = array('status' => $this->_done, 'error' => $prq->errorCode() ); $JsonObject = json_encode($json); }else{ $json = array('status' => $this->_done); $JsonObject = json_encode($json); } echo $JsonObject; } public function addRegenerationNote($noteType, $plantId , $regenerationDate) { $this->_noteType = $noteType; $this->_plantId = $plantId; $this->_regenerationDate = $regenerationDate; $db = mysql_init(); $sql = "INSERT INTO notes ( noteId , noteType, plantId, regenerationDate ) VALUES ('', ?, ?, ?)"; $prq = $db->prepare($sql); $prq->bindValue(1,$this->_noteType,PDO::PARAM_STR); $prq->bindValue(2,$this->_plantId,PDO::PARAM_INT); $prq->bindValue(3,$this->_regenerationDate,PDO::PARAM_STR); ($prq->execute()) ? $this->_done = 1 : $this->_done = 0; if ($this->_done == '0') { $json = array('status' => $this->_done, 'error' => $prq->errorCode() ); $JsonObject = json_encode($json); }else{ $json = array('status' => $this->_done); $JsonObject = json_encode($json); } echo $JsonObject; } ..... As you can see i call in every method the mysql_init() function. Is it possible to call it only once at the start of the class and then just use the $db var ? Thank you. Quote Link to comment Share on other sites More sharing options...
Solution AbraCadaver Posted December 10, 2013 Solution Share Posted December 10, 2013 (edited) private $_db; public function __construct() { $this->_db = mysql_init(); } Or pass it in: private $_db; public function __construct($db) { $this->_db = $db; } $db = mysql_init(); $note = new Note($db); Then anytime you need this in your class just use $this->_db Edited December 10, 2013 by AbraCadaver Quote Link to comment Share on other sites More sharing options...
netpumber Posted December 10, 2013 Author Share Posted December 10, 2013 Thank you very much.. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 10, 2013 Share Posted December 10, 2013 No prob. I'm not a super OOP guy but I think the second way is the preferred method (dependency injection). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.