Jump to content

one common variable in php class


netpumber
Go to solution Solved by AbraCadaver,

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
Share on other sites

  • Solution
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 by AbraCadaver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.