OO in PHP (2)
续 上一篇 的内容;不过也不去解说什么了,看例子:
class Validator {
private $cur;
public function set($name) {
if (!isset($_POST[$name])) throw new Exception();
$this->cur = $_POST[$name];
return $this;
}
public function sanitize() {
$this->cur = trim($this->cur);
return $this;
}
public function isValidNick() {
// Do something you like to....
// If something goes wrong, just throw a exception.
return $this;
}
public function fetch() {
return $this->cur;
}
}
$v = new Validator();
try {
$nick = $v->set('usr_name')->sanitize()->isValidNick()->fetch();
// Do something after sanitize.
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}

2007-12-16 12:51:48 +0800, fcicq said,