It appears that I can only make global variables inside the constructor using $this->
.
[php]
protected static $db;
protected static $realanswer;
public function __construct($database) {
$this->db = $database;
}
[/php]
I am able to set my connection right there. No problem, at all using the above technique. When I try to replicate this technique in a method and then try to call the method it is null. I set it right here:
[php]
public function getQuizName() {
$sql = 'Select Japaneseword From JapaneseDefinition;';
$stmt = $this->db->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_OBJ);
$sizeofArray = sizeof($data);
$randomNumber = rand(0, $sizeofArray - 1);
if (isset($data[$randomNumber]->Japaneseword)) {
$answer = $data[$randomNumber]->Japaneseword;
//this is where I am having the trouble
$this->realanswer = $answer;
echo $data[$randomNumber]->Japaneseword;
} else {
$QuizName = new Quiz($db1);
$QuizName->getQuizName($db1);
}
public function showAnswer(){
print_r($this->realanswer);
}
}
//I call up the method right here
$question->showAnswer();
[/php]
I get nothing using this way. I can set realanswer inside the constuctor and access it easily when I call the showAnswer method. This only works when I set it in the construct if I try to set it in any other method it shows null. I hope this makes sense.