Answer the question
In order to leave comments, you need to log in
How to use method to set variable value in php script?
There is a class:
class Comment
{
public $name = "Name";
public function getName() {
return $this->name;
}
public function setName($var) {
$this->name = sanitize($var);
}
public function sanitize($str) {
$str = trim($str);
$str = nl2br($str);
return $str;
}
public function setName($var) {
$this->name = $var;
}
Answer the question
In order to leave comments, you need to log in
$this->sanitize();
the method belongs to the class, and not separately lying around
In general, it is better to do this through __set, __get, so that there are no ugly entries like $obj -> set/getName, but simply access name as if it were a field of an object. Well, sanitize needs to be called as static::sanitize. Just add static to the sanitize method. Oh, and you definitely need to make PRIVATE $name, otherwise the meaning of encapsulation is lost.
Thank you very much! private, of course, I'll remove it. I put it in public, because for three hours I've been trying to figure out what it is!!!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question