drakal30 Posted May 16, 2007 Share Posted May 16, 2007 I am running php version 5.0.4 and I cannot seem to get classes to work properly. Here is my class <?php class regUser { var $id; var $name; var $rights; function regUser($id,$name,$rights) { $_SESSION['myID'] = $id; $_SESSION['name'] = $name; $_SESSION['rights'] = $rights; $info->id = $id; $info->name = $name; $info->rights = $rights; } function dspUser() { $return = "Name " . $info->$name . " id is " . $this->id . " Rights are " .$this->$rights; return $return; } } ?> I call the contructor like this $user = new regUser($id,$name,$rights); when I try and and display the data I get nothing from the variables they do not return. I call it like this. echo $user->dspUser(); it seems the variable is not being passed with the object class. Any ideas what I am doing wrong? Thanks! Link to comment https://forums.phpfreaks.com/topic/51741-help-with-php-classes/ Share on other sites More sharing options...
kathas Posted May 16, 2007 Share Posted May 16, 2007 try changing the code like this... <?php class regUser { var $info = array(); function __construct($id,$name,$rights) { $_SESSION['myID'] = $id; $_SESSION['name'] = $name; $_SESSION['rights'] = $rights; $info['id'] = $id; $info['name'] = $name; $info['rights'] = $rights; } function dspUser() { $return = "Name " . $info['name'] . " id is " . $info['id'] . " Rights are " .$info['rights']; return $return; } } ?> Link to comment https://forums.phpfreaks.com/topic/51741-help-with-php-classes/#findComment-254865 Share on other sites More sharing options...
trq Posted May 16, 2007 Share Posted May 16, 2007 Try using $this instead of $info. $this is a special reference within a class that points to the instantiated object itself. There are also a few other changes I would make seeing as your using php5. Here... <?php class regUser { private $id; private $name; private $rights; function __construct($id,$name,$rights) { $_SESSION['myID'] = $id; $_SESSION['name'] = $name; $_SESSION['rights'] = $rights; $this->id = $id; $this->name = $name; $this->rights = $rights; } public function dspUser() { return "Name " . $this->name . " id is " . $this->id . " Rights are " .$this->rights; } } $user = new regUser(1,'foo','admin'); echo $user->dspUser(); ?> PS: I dont see any call to session_start()? Link to comment https://forums.phpfreaks.com/topic/51741-help-with-php-classes/#findComment-254885 Share on other sites More sharing options...
drakal30 Posted May 17, 2007 Author Share Posted May 17, 2007 Hey thanks a lot guys it worked, it was the initialization of the array that was it. In all the examples I have seen they never initialized the array first. Stupid of me. Thanks again for the good info! Link to comment https://forums.phpfreaks.com/topic/51741-help-with-php-classes/#findComment-255020 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.