Note that there are some explanatory texts on larger screens.

plurals
  1. POphp: two objects from the same class work independent of each other
    primarykey
    data
    text
    <p>Good morning,</p> <p>I would like the code in my controller to look something like this:</p> <pre><code>&lt;?php $class = new sanitizeInput() $string1 = $class -&gt; input($_POST[name]) -&gt; mysql_escape(); $string2 = $class -&gt; input($_POST[age]) -&gt; mysql_escape(); print " String1: $string1 &lt;br /&gt; String2: $string2" ?&gt; </code></pre> <p>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.</p> <p>Sure, I know I can instantiate twice, but I would like to use the same object if possible.</p> <p>It would be great if my class:</p> <ul> <li>Instantiate once,</li> <li>Set input,</li> <li>Tell it to mysql_escape, and return __toString to $string1.</li> <li>Set input leaving $string2 alone, mysql_escape and return __toString string to $string2.</li> </ul> <p>EDIT: This is my full code as requested by comment:</p> <pre><code>$name = $sanitize -&gt; setInput($name) -&gt; stripTags() -&gt; mySql() -&gt; replaceLinks('[ En webadresse ble sensurert her ]') -&gt; trimWhitespace(); $age = $sanitize -&gt; setInput($age) -&gt; stripTags() -&gt; mySql() -&gt; replaceLinks('[ En webadresse ble sensurert her ]') -&gt; trimWhitespace(); class Sanitizer { protected $_data; public function setInput($input) { $this -&gt; _data = $input; return $this; } public function stripTags($array = NULL) { if (!is_null($array) and is_array($array)) { $allowedTags = implode('', $array); $this -&gt; _data = strip_tags($this -&gt; _data, $allowedTags); } else { $this -&gt; _data = strip_tags($this -&gt; _data); } return $this; } public function mySql() { $this -&gt; _data = mysql_escape_string($this -&gt; _data); return $this; } public function replaceLinks($replacement = NULL) { if (is_null($replacement)) { $replacement = '[ Potential web-address censored here ]'; } $this -&gt; _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 -&gt; _data); return $this; } public function trimWhitespace() { $this -&gt; _data = trim($this -&gt; _data); return $this; } protected function __toString() { $str = $this -&gt; _data; return $str; } } </code></pre> <p>Thank you for your time.</p> <p>Kind regards,<br /> Marius</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload