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?