Reputation: 7
I am getting this error (Notice: Trying to get property 'X' of non-object) while trying to access to the members of one object that I know that exist ($newStudent = new student();) . Does anybody knows what can be the issue?
It fails while running this part:
if (isset($_POST['submit'])) {
$newStudent = new student();
if(is_object($newStudent)){
$newStudent->settForNavn(htmlentities($_POST['fornavn']));
$newStudent->settEtterNavn(htmlentities($_POST['etternavn']));
$newStudent->settEpost(htmlentities($_POST['epost']));
$newStudent->settMobil(htmlentities($_POST['mobil']));
$id = $studReg->leggTilStudent($newStudent);
print ("<a href=" . $_SERVER['PHP_SELF'] . ">Tilbake</a>");}
}
Errors:
Notice: Trying to get property 'Fornavn' of non-object in C:\xampp\htdocs\lab3\oppgave\student.class.php on line 42
Notice: Trying to get property 'Etternavn' of non-object in C:\xampp\htdocs\lab3\oppgave\student.class.php on line 45
Notice: Trying to get property '[email protected]' of non-object in C:\xampp\htdocs\lab3\oppgave\student.class.php on line 55
Notice: Trying to get property 'Mobilnummer' of non-object in C:\xampp\htdocs\lab3\oppgave\student.class.php on line 48
StudentRegister.php
<?php
spl_autoload_register(function ($class_name) {
require_once $class_name . '.class.php';
});
require_once 'auth.php';
$studReg = new StudentRegister($db);
if (isset($_POST['submit'])) {
$newStudent = new student();
if(is_object($newStudent)){
$newStudent->settForNavn(htmlentities($_POST['fornavn']));
$newStudent->settEtterNavn(htmlentities($_POST['etternavn']));
$newStudent->settEpost(htmlentities($_POST['epost']));
$newStudent->settMobil(htmlentities($_POST['mobil']));
$id = $studReg->leggTilStudent($newStudent);
print ("<a href=" . $_SERVER['PHP_SELF'] . ">Tilbake</a>");}
}
else if(isset($_GET['id']) && ctype_digit($_GET['id']))
{
$id = intval($_GET['id']);
if($student = $studReg->visStudent($id)) {
print("Navn: " . $student->hentNavn() . "<br />\n");
print("Klasse: ". $student->hentKlasse() . "<br />\n");
print("Mobil: " . $student->hentMobil() . "<br />\n");
print("Epost: ". $student->hentEpost() . "<br />\n");
print ("<a href=" . $_SERVER['PHP_SELF'] . ">Tilbake</a>");
}
else {
// Ingen poster
echo "Beklager, fant ingen poster!";
}
}
else {
print("Studenter:<br>");
$studenter = $studReg->visAlle();
foreach ($studenter as $student )
{
print("<a href=" . $_SERVER['PHP_SELF'] . "?id=" . $student->hentId() . ">". $student->hentNavn() . "</a><br/>\n");
}
print("<br>");
print("<br>");
print("Add a new student to the DB:<br>");
print('
<div class= "black">
<form method="post">
<input type="hidden" name="id" value=""><br>
<div>
<label for="Fornavn: "</label>
<input type="text" name="fornavn" value="Fornavn" required><br>
</div>
<br>
<div>
<label for="Etternavn: "</label>
<input type="text" name="etternavn" value="Etternavn" required><br>
</div>
<br>
<div>
<label for="E-post "</label>
<input type="email" name="epost" value="E-post" required><br>
</div>
<br>
<div>
<label for="Mobilnummer "</label>
<input type="text" name="mobil" value="Mobilnummer" required><br>
</div>
<br>
<br>
<button type="submit" name="submit">Submit</button>
</form>
</div>
');
}
?>
student.class.php
<?php
class student {
private $id;
private $etternavn;
private $fornavn;
private $klasse;
private $mobil;
private $www;
private $epost;
function __construct() {
}
//Getters
function hentId() {
return $this->id;
}
function hentNavn() {
return $this->fornavn . " " . $this->etternavn;
}
function hentForNavn() {
return $this->fornavn;
}
function hentEtterNavn() {
return $this->etternavn;
}
function hentMobil() {
return $this->mobil;
}
function hentKlasse() {
return $this->klasse;
}
function hentEpost() {
return $this->epost;
}
function hentURL() {
return $this->www;
}
//Setters
function settForNavn($fornavn) {
$this->fornavn->$fornavn;
}
function settEtterNavn($etterNavn) {
$this->etternavn->$etterNavn;
}
function settMobil($mobil) {
$this->mobil->$mobil;
}
function settKlasse($klasse) {
$this->klasse->$klasse;
}
function settEpost($epost) {
$this->epost->$epost;
}
}
Upvotes: 0
Views: 486
Reputation: 137
In your student class, the setters should be set like the following
//Setters
function settForNavn($fornavn) {
$this->fornavn = $fornavn;
}
function settEtterNavn($etterNavn) {
$this->etternavn = $etterNavn;
}
function settMobil($mobil) {
$this->mobil = $mobil;
}
function settKlasse($klasse) {
$this->klasse = $klasse;
}
function settEpost($epost) {
$this->epost = $epost;
}
Upvotes: 1