Mam dwie klasy
Pytanie
<?php
class Pytanie {
private $id;
private $tresc;
private $warianty = array(); private $poprawne = array(); private $przedmiot;
private $autor;
public $dobre=0;
public $zle=0;
function __construct($id) {
connect_db();
$sql = "SELECT id, tresc, warianty, poprawne, przedmiot, autor FROM pytania WHERE id = '$id'";
$this->id = $wartosc_z_bazy['id'];
$this->tresc = $wartosc_z_bazy['tresc'];
$this->warianty = explod($wartosc_z_bazy['warianty']);
$this->poprawne = explod($wartosc_z_bazy['poprawne']);
$this->przedmiot = $wartosc_z_bazy['przedmiot'];
$this->autor = $wartosc_z_bazy['autor'];
}
}
function getId () {
return $this->id;
}
function setTresc ($parametr) {
$this->tresc = $parametr;
return 1;
}
function getTresc () {
$this->tresc = convert_tex($this->tresc);
return $this->tresc;
}
function setWarianty ($parametr) {
foreach($parametr as $temp) {
$i=0;
$this->warianty[$i] = $temp;
$i++;
}
return 1;
}
function getWarianty () {
$i=0;
foreach($this->warianty as $res) {
$this->warianty[$i] = convert_tex($res);
$i++;
}
}
function showWarianty () {
$temp = "";
$result = "";
foreach($this->warianty as $temp) {
$result = ($result.'<br><input type="checkbox" name="'.$this->id.'[]" value="'.$temp.'" />'.$temp);
}
return $result;
}
function setPoprawne ($parametr) {
foreach($parametr as $temp) {
$i=0;
$this->poprawne[$i] = $temp;
$i++;
}
return 1;
}
function getPoprawne () {
$i=0;
foreach($this->poprawne as $res) {
$this->poprawne[$i] = convert_tex($res);
$i++;
}
}
function showPoprawne () {
$temp = "";
$result = "";
foreach($this->poprawne as $temp) {
$result = ($result.'<br>'.$temp);
}
return $result;
}
function setPrzedmiot ($parametr) {
$this->przedmiot = $parametr;
return 1;
}
function getPrzedmiot () {
return $this->przedmiot;
}
function setAutor ($parametr) {
$this->autor = $parametr;
return 1;
}
function getAutor () {
return $this->autor;
}
function modyfikuj($tresc, $warianty, $poprawne, $przedmiot, $autor) {
$this->setTresc($tresc);
$this->setWarianty($warianty);
$this->setPoprawne($poprawne);
$this->setPrzedmiot($przedmiot);
$this->setAutor($autor);
connect_db();
$warianty = inplode($warianty);
$poprawne = inplode($poprawne);
$sql = "UPDATE `pytania` SET `tresc` = '$tresc', `warianty` = '$warianty', `poprawne` = '$poprawne', `przedmiot` = '$przedmiot', `autor` = '$autor' WHERE `pytania`.`id` = '$this->id';";
}
function sprawdz($zaznaczone) {
foreach($this->poprawne as $temp1) {
foreach($zaznaczone as $temp2) {
if($temp1==$temp2) $this->dobre++;
else $this->zle++;
}
}
}
}
?>
i Test
<?php
class Test {
private $id;
private $nazwa;
private $przedmiot;
private $czas;
private $nr_pyt;
private $pytania = array(); private $status;
function getId () {
return $this->id;
}
function setNazwa($parametr) {
$this->przedmiot = $parametr;
return 1;
}
function getNazwa () {
return $this->przedmiot;
}
function setPrzedmiot($parametr) {
$this->przedmiot = $parametr;
return 1;
}
function getPrzedmiot () {
return $this->przedmiot;
}
function setCzas ($parametr) {
$this->czas = $parametr;
return 1;
}
function getCzas () {
return $this->czas;
}
function setPytania ($parametr) {
$i=0;
foreach($parametr as $temp) {
$this->pytania[$i] = $temp;
}
return 1;
}
function getPytania () {
}
function setStatus ($parametr) {
$this->status = $parametr;
return 1;
}
function getStatus () {
return $this->status;
}
function __construct($id) {
connect_db();
$sql = "SELECT id, przedmiot, czas, pytania, status FROM testy WHERE id = '$id'";
$this->id = $wartosc_z_bazy['id'];
$this->przedmiot = $wartosc_z_bazy['przedmiot'];
$this->czas = $wartosc_z_bazy['czas'];
$this->nr_pyt = explod($wartosc_z_bazy['pytania']);
$this->status = $wartosc_z_bazy['status'];
}
$i=0;
foreach($this->nr_pyt as $temp) {
$this->pytania[$i] = new Pytanie($temp);
$i++;
}
}
function uruchom_test () {
$this->setStatus(1);
connect_db();
$sql = "UPDATE `testy` SET `status` = '$this->status' WHERE `pytania`.`id` = '$this->id';";
}
function wyswietl_test () {
echo('<div class="test_title">'.$this->nazwa.'</div>'); echo('<div class="time">'.$this->czas.'</div>');
$i=0;
foreach($this->pytania as $temp) {
<div class="podzial_gora"></div>
<div class="tresc">'.$this->pytania[$i]->getTresc().'</div>
<div class="warianty">'.$this->pytania[$i]->getWarianty().'</div>
<div class="podzial_dol></div>"
</div>');
$i++;
}
}
function zakoncz_test () {
$i=0;
foreach($this->pytania as $temp) {
$this->pytania[$i]->sprawdz($_POST[$this->pytania->getId()]);
$i++;
}
}
function dezaktywuj_test () {
$this->setStatus(0);
return 1;
}
function wyswietl_wynik () {
echo ($this->pytania->dobre); echo ($this->pytania->zle);
}
function modyfikuj_test ($nazwa, $przedmiot, $czas, $pytania) {
$this->setNazwa($nazwa);
$this->setPytania($pytania);
$this->setPrzedmiot($przedmiot);
$this->setCzas($czas);
connect_db();
$pytania = inplode($pytania);
$sql = "UPDATE `testy` SET `nazwa` = '$nazwa', `pytania` = '$pytania', `czas` = '$czas', `przedmiot` = '$przedmiot' WHERE `testy`.`id` = '$this->id';";
}
}
?>
Problem polega na tym że w konstruktorze klasy test tworze tablice obiektów typu pytanie i choćby przy próbie użycia metody wyswietl_test() wyskakuje błąd:
Fatal error: Call to a member function getTresc() on a non-object in ....... on line 122
Co tu jest źle jak powinien wyglądać konstruktor aby obiekty zostały utworzone poprawnie