早上好,
我希望我的控制器中的代码看起来像这样:
<?php
$class = new sanitizeInput()
$string1 = $class -> input($_POST[name]) -> mysql_escape();
$string2 = $class -> input($_POST[age]) -> mysql_escape();
print "
String1: $string1 <br />
String2: $string2"
?>
似乎在我的 sanitizeInput 类中,对 $string2 的任何更改都会应用于 $string1。我可以通过什么方式改变这一点?我希望在类中进行更改,以使我的控制器尽可能易于阅读。
当然,我知道我可以实例化两次,但如果可能的话,我想使用同一个对象。
如果我的课:
- 实例化一次,
- 设置输入,
- 告诉 mysql_escape,并将 __toString 返回到 $string1。
- 设置输入,只留下 $string2,mysql_escape 并将 __toString 字符串返回到 $string2。
编辑:这是我根据评论要求的完整代码:
$name = $sanitize -> setInput($name) -> stripTags() -> mySql() -> replaceLinks('[ En webadresse ble sensurert her ]') -> trimWhitespace();
$age = $sanitize -> setInput($age) -> stripTags() -> mySql() -> replaceLinks('[ En webadresse ble sensurert her ]') -> trimWhitespace();
class Sanitizer {
protected $_data;
public function setInput($input) {
$this -> _data = $input;
return $this;
}
public function stripTags($array = NULL) {
if (!is_null($array) and is_array($array)) {
$allowedTags = implode('', $array);
$this -> _data = strip_tags($this -> _data, $allowedTags);
}
else {
$this -> _data = strip_tags($this -> _data);
}
return $this;
}
public function mySql() {
$this -> _data = mysql_escape_string($this -> _data);
return $this;
}
public function replaceLinks($replacement = NULL) {
if (is_null($replacement)) {
$replacement = '[ Potential web-address censored here ]';
}
$this -> _data = preg_replace('~[a-z0-9:/._-]+\.(biz|com|edu|gov|info|mil|net|org|as|eu|no|se|uk)[/a-z]{0,}~i', $replacement, $this -> _data);
return $this;
}
public function trimWhitespace() {
$this -> _data = trim($this -> _data);
return $this;
}
protected function __toString() {
$str = $this -> _data;
return $str;
}
}
感谢您的时间。
亲切的问候,
马吕斯