How to use the function within the class??

class UserRegister{


public $password = '';


public function escape($val)
{
return mysql_real_escape_string($val);
}

public function setPassword($val)
{
$this->password = $val;
$val = escape($this->password);
$this->password = $val;
}

public function getPassword()
{
echo $this->password;   
}

}

$obj = new UserRegister;
echo $obj->setPassword("dsadsadasdas'das");
echo $obj->getPassword();

But it prompt call to undefined function escape(). How can I apply escape() which is return mysql_real_escape_string() to setPassword? I want to let mysql_real_escape_string filter it before it set to $password.

Recommended Answers

All 2 Replies

echo $obj->setPassword("dsadsadasdas'das"); this is wrong. Yo don't return anything, so don't output this. You're setting, not getting. What seems to be the error?

Thank you phorce. I got the solution now. I just change from this

public function setPassword($val)
{
$this->password = $val;
$val = escape($this->password);
$this->password = $val;
}

to

public function setPassword($val)
{
$this->password = $this->escape($val);
}

and it's work! .. My output is dsadsadasdas\'das now..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.