Jump to content

one common variable in php class


netpumber

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/284688-one-common-variable-in-php-class/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.