Jump to content

Recommended Posts

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.