$this and making global variables

It appears that I can only make global variables inside the constructor using $this->

protected static $db;
protected static $realanswer;

public function __construct($database) {
            $this->db = $database;


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:
public function getQuizName() {

            $sql = 'Select Japaneseword From JapaneseDefinition;';
            $stmt = $this->db->prepare($sql);
            $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);
            public function showAnswer(){

//I call up the method right here


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.

when you say you get nothing, what error posts in your your errorlog in the www folder if not on the screen?

Sponsor our Newsletter | Privacy Policy | Terms of Service