Jump to content

any idea on whats going wrong calling this property?


Guest

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();

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.