Undefined variable PHP POO

0

I'm just starting out in the PHP world, and I had a problem that I did not find a solution for, I looked everywhere and did not find something that could help me. My situation is as follows, I am working on a questionnaire and at the moment of instantiating the object the% Undefined variable error jumps. this is the object:

<?php
    class quiz {
      public $name;
      public $author;
      public $subject;
      public $questions = [];

      public function __construct($n,$a,$s){
        $this->name = $n;
        $this->author = $a;
        $this->subject =$s;
      }
      public function addq($q){
        $this->questions[] = new question($q);
      }
      public function getName(){
        echo  $this->name;
      }
    }
    class question {
     public $question;
     public $answers = [];

     public function __construct($q) {
       $this->question = $q;
     }

     public function adda($a, $v){
       $this->answers[] = new answer($a, $v);
     }

     public function getQuestion(){
       echo  $this->question;
     }
   }
   class answer {
     public $answer = [];

     public function __construct($a, $v) {
        $this->answer[] = array(
         'risposta' => $a,
         'valore' => $v,
        );
     }

      public function getAnswer(){
        //codificazione in json per una lettura più efficace
        $tmp = json_encode(array_column($this->answer, 'risposta'), TRUE);
        //sotto stringa per prendere unicamente il valore della risposta senza [""] inserito da json
        $tmp = substr($tmp,2,strlen($tmp)-4);
        //stampa della variabile preparata in precedenza
         print_r($tmp);
      }
   }
?>

and instantiate the object quiz aca:

   <?php
      session_start();

     //reindirizzamento
     $urlRedirect = 'http://localhost:8888/prova%20ogetti/view/aggiungidomanda.php';
     header('Location:'.$urlRedirect);

     //autoload di classi esistenti all'interno del progetto
     function __autoload($classname) {
        $filename = "../model/". $classname .".php";
        include_once($filename);
     }

     //presa de  dati inviati tramite post
     $name = $_POST['nome'];
     $author = $_POST['autore'];
     $subject = $_POST['materia'];
     //$image = $_POST['file'];

     //creazione del ogetto quiz
     $obj = new quiz($name, $author, $subject);

     //inizializzazione delle variabili di sessione
     $_SESSION['obj'] = serialize($obj);
     $_SESSION['state'] = "q";
     session_write_close();
  ?>

The problem arises when I try to add the question with the respective answers in another file:

   <?php
     session_start();
    //iniziamo la sessione
    //reindirizzamento automatico
     $urlRedirect = 'http://localhost:8888/prova%20ogetti/view/aggiungidomanda.php';
    //header('Location:'.$urlRedirect);

    include("../model/quiz.php");


    //prendiamo l'ogetto "quiz" creato in precedenza dalla variabile di sessione
    $obj = unserialize($_SESSION['obj']);

    // prendiamo le variabile passate tramite post dalla vista aggiungidomanda
    $domanda = $_POST['domanda'];
    $risp1 = $_POST['risp1'];
    if($_POST['val1'] = null){$val1 = "false";}else{$val1 = "true";};
    $risp2 = $_POST['risp2'];
    if($_POST['val2'] = null){ $val2 = "false";}else{$val2 = "true";};
    $risp3 = $_POST['risp3'];
    if($_POST['val3'] = null){ $val3 = "false";}else{$val3 = "true";};
    $risp4 = $_POST['risp4'];
    if($_POST['val4'] = null){ $val4 = "false";}else{$val4 = "true";};

    $obj->addq($domanda);

    //aggiungo la risposta e il valore corrispondente nella domanda rispettiva
    $obj->$questions[count($obj->$questions)-1]->adda($risp1,$val1);
    $obj->$questions[count($obj->$questions)-1]->adda($risp2,$val2);
    $obj->$questions[count($obj->$questions)-1]->adda($risp3,$val3);
    $obj->$questions[count($obj->$questions)-1]->adda($risp4,$val4);

    session_write_close();
  ?>

the error that comes out would be this: Undefined variable: questions in ... on line 34 What am I doing wrong?

    
asked by Federico 14.11.2017 в 14:11
source

1 answer

0

The correct way to access an instance property questions would be

$obj->questions

And not

$obj->$questions

Anyway, that serializing the classes to put them in a session variable is a bit of playing with a black box. It can work for this particular example, but there are objects that are not serializable and if a class constitutes or uses one of those objects (for example the connection to a BBDD) there are things that are going to be broken.

Secondly (and this has no relation to the error of your question), you are declaring an autoloader:

 function __autoload($classname) {
    $filename = "../model/". $classname .".php";
    include_once($filename);
 }

Which allows you to instantiate quiz without doing the manual include.

 $obj = new quiz($name, $author, $subject);

But it would not allow you to instantiate an object question or answer without manual inclusion of quiz.php as you are doing in the third script.

If at this moment you find it is because as they are all in the same file, the manual inclusion in the third script anecdotally brings the declaration of the other two classes.

It would be healthier to have quiz.php , question.php and answer.php each containing the declaration of a class, and in all your other scripts make an include_once of the script that declares the autoloader. This is just a design suggestion, but it will make your life easier in the future.

    
answered by 14.11.2017 / 17:05
source