Jump to content

any idea on whats going wrong calling this property?


Recommended Posts

I'm fairly new to OOP, could you help me fix this? Thanks!

 

class register{

//Setting up Variables, you can add more, for example EMAIL.

private $_username;
private $_password;

//Giving the variables values
function __construct($u_name, $p_word)
{
    $this->_username = $u_name;
    $this->_password = $p_word;
   

}

 

if(isset($_POST['submit']))
{
$reg = new register($_POST['username'], $_POST['password']);

echo $reg->u_name;
}

 

Notice: Undefined property: register::$u_name in C:\wamp\www\register.php on line 23

There is no $u_name property in this object.

 

Maybe you want something like this:

 

class register{

//Setting up Variables, you can add more, for example EMAIL.

private $_username;
private $_password;

//Giving the variables values
function __construct($u_name, $p_word)
{
    $this->_username = $u_name;
    $this->_password = $p_word;
}

  public function getUsername() {
    return $this->_username;
  }
}

//then

echo $reg->getUsername();

 

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.