Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 757043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:18:59+00:00 2026-05-14T15:18:59+00:00

Good morning, I would like the code in my controller to look something like

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:19:00+00:00Added an answer on May 14, 2026 at 3:19 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morgning, I would like to be able to analyze text where I am
Good morning, afternoon, evening or night (depending on your timezone). This is just a
Good morning, I am about to start writing an Excel add-in for Excel 2002.
Good morning. I have an XML file which contains lists of warning and errors
Good morning, I am the developer of a medium sized PDA application that will
Good morning, I work in a small shop (only two of us) and we
Good morning, I am working on a C# winform application that is using validation
Good morning, Apologies for the newbie question. I'm just getting started with ASP.NET internationalization
Good afternoon, This should be an easy one. I've done the cookie-cutter default ASP.NET
Good evening I've got a little problem with my DataGridView in a .NET Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.