leechael.orgHome

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

2 comments

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

肯定慢 :D

2007-12-17 10:05:14 +0800, Hu, Zhenghui said,

真的觉得,PHP的->看不习惯。呵呵。

Wanna say something?