0

I started with php a bit ago, doing a website for my university, but for some reason i can't get my constructor to work.

User.class.php

class User {

public $id="";
public $name="";
public $vorname="";
public $matrikelnummer="";
public $status="";
public $wl_platz="";
public $modul="";
public $versuch="";

public function __construct($id, $name, $vorname, $matrikelnummer, $status, $wl_platz, $modul, $versuch) 
{
    $this->$id=$id;
    $this->$name=$name;
    $this->$vorname=$vorname;
    $this->$matrikelnummer=$matrikelnummer;
    $this->$status=$status;
    $this->$wl_platz=$wl_platz;
    $this->$modul=$modul;
    $this->$versuch=$versuch;
}

in main.php

include_once('include/User.class.php');         
$user=new User($id, $name, $vorname, $mn, $status, $wl_platz, $modul, $versuch);

the variables in the call are strings that were set before, obviously

if i do echo serialize($user);

i get

O:4:"User":16:{s:2:"id";s:0:"";s:4:"name";s:0:"";s:7:"vorname";s:0:"";
s:14:"matrikelnummer";s:0:"";s:6:"status";s:0:"";s:8:"wl_platz";
s:0:"";s:5:"modul";s:0:"";s:7:"versuch";s:0:"";s:1:"1";s:1:"1";s:9:
"alexander";s:9:"alexander";s:4:"jung";s:4:"jung";s:5:"34525";s:5:"34525"
;s:19:"registered for exam";s:19:"registered for exam";
s:4:"TODO";s:4:"TODO";s:3:"PAD";s:3:"PAD";s:1:"3";s:1:"3";}

so it just be some sort of working, but if i do echo $user->name; i get empty output. pretty sure there must be a small mistake somewhere, but can't find it... (and i did some searching and all)

2
  • 2
    Are you using variable properties intentionally or it's just a typo? $this->$id Commented Feb 1, 2018 at 9:59
  • @ÁlvaroGonzález You are right. Commented Feb 1, 2018 at 10:01

1 Answer 1

2

You're not setting your attributes by $this->$id.

It's like you're setting $this->yourvalueunder$id = ...

Change this:

public function __construct($id, $name, $vorname, $matrikelnummer, 

$status, $wl_platz, $modul, $versuch) 
{
    $this->$id=$id;
    $this->$name=$name;
    $this->$vorname=$vorname;
    $this->$matrikelnummer=$matrikelnummer;
    $this->$status=$status;
    $this->$wl_platz=$wl_platz;
    $this->$modul=$modul;
    $this->$versuch=$versuch;
}

To this:

public function __construct($id, $name, $vorname, $matrikelnummer, $status, $wl_platz, $modul, $versuch) 
{
    $this->id=$id;
    $this->name=$name;     // '$' sign in attribute name
    $this->vorname=$vorname;
    $this->matrikelnummer=$matrikelnummer;
    $this->status=$status;
    $this->wl_platz=$wl_platz;
    $this->modul=$modul;
    $this->versuch=$versuch;
}
Sign up to request clarification or add additional context in comments.

1 Comment

lol. embarrassing that i had to do this as my first post on the site, just kept ignoring the $$$ in my head i guess :) thanks for the quick and obvious answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.