php:来自同一类的两个对象彼此独立工作

发布于 2024-08-31 10:16:02 字数 2251 浏览 3 评论 0原文

早上好,

我希望控制器中的代码看起来像这样:

<?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;
    }
}

谢谢您的时间。

亲切的问候,
马吕斯

Good morning,

I would like the code in my controller to look something like this:

<?php
$class = new sanitizeInput()

$string1 = $class -> input($_POST[name]) -> mysql_escape();
$string2 = $class -> input($_POST[age]) -> mysql_escape();

print "
     String1: $string1 <br />
     String2: $string2"
?>

It seems with my sanitizeInput class, any change to $string2 is applied to $string1.
What ways can I change this? I would preferably like to make the changes within the class to make my controller as easily read as possible.

Sure, I know I can instantiate twice, but I would like to use the same object if possible.

It would be great if my class:

  • Instantiate once,
  • Set input,
  • Tell it to mysql_escape, and return __toString to $string1.
  • Set input leaving $string2 alone, mysql_escape and return __toString string to $string2.

EDIT:
This is my full code as requested by comment:

$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;
    }
}

Thank you for your time.

Kind regards,
Marius

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

著墨染雨君画夕 2024-09-07 10:16:02

$string1$string2 将是对同一对象的引用,直到您尝试将值转换为字符串,因此您所做的任何更改都将应用于这两个字符串。我认为您必须将对象显式转换为字符串才能防止这种情况,例如,

$string1 = (string) $class -> input($_POST['name']) -> mysql_escape();
$string2 = (string) $class -> input($_POST['age']) -> mysql_escape();

我不确定在这里使用“流畅”接口是否合适,因为如果您愿意,您并不真正希望对象在调用之间维护状态同时在多个地方使用同一个实例。最好为每个字符串使用不同的对象。

$string1 and $string2 will be references to the same object right up until you try to convert the value to a string, so any changes you make will be applied to both strings. I think you would have to explicitly convert the object to a string to prevent this, e.g.

$string1 = (string) $class -> input($_POST['name']) -> mysql_escape();
$string2 = (string) $class -> input($_POST['age']) -> mysql_escape();

I'm not sure using a 'fluent' interface is appropriate here because you don't really want the object to be maintaining state between calls if you want to use the same instance in multiple places at the same time. It would be better to use a different object for each string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文