dt97868 2010-05-04 14:20
浏览 57
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • doudanglang5826 2010-05-04 14:51
    关注

    $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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多