3

I am writing a class to sanitize strings passed to PHP through an ajax call, when I pass a string into this class it works fine but passing the array as a reference and it won't work.

class Sanitize {

    public static function clean (&$str) {
        self::start($str);
    }

    public static function cleanArray (&$array) {
        if (self::arrayCheck($array)) {
            foreach ($array as $key => $value) {
                if (self::arrayCheck($value)) {
                    self::cleanArray($value);
                } else {
                    self::clean($value);
                }
            }
        } else {
            throw new Exception ('An array was not provided. Please try using clean() instead of cleanArray()');
        }
    }

    private static function start (&$str) {
        $str .= '_cleaned';
    }

    private static function arrayCheck ($array) {
        return (is_array($array) && !empty($array));
    }
}

Test Code:

$array = array(
    'one' => 'one',
    'two' => 'two',
    'three' => 'three',
    'four' => 'four'
);
echo print_r($array, true) . PHP_EOL;
Sanitize::cleanArray($array);
echo print_r($array, true) . PHP_EOL;

Output:

Array
(
    [one] => one
    [two] => two
    [three] => three
    [four] => four
)

Array
(
    [one] => one
    [two] => two
    [three] => three
    [four] => four
)

Is there something I am missing, or is it not possible to nest reference passes in PHP?

6
  • 2
    I think you're losing the reference when doing the foreach. Try using this: foreach ($array as &$key => &$value) Commented Dec 4, 2014 at 15:57
  • Oh that's perfect, it doesn't work on the &key (throwing a fatal error) but the value can be passed as a reference. Thank you sir. Please post as an answer and I will accept. Commented Dec 4, 2014 at 15:59
  • Avoid passing paramaters by reference. 99% of the time there is a better way Commented Dec 4, 2014 at 16:03
  • 1
    @Konstantin Please elaborate on why passing as a reference is almost always worse. In my mind, altering the value of a variable without returning and reassigning is cleaner. Is it an efficiency or system resource issue? I ask because I am open to learn new things. Commented Dec 4, 2014 at 16:05
  • 1
    Jonathan, technical efficiency practically doesn't matter in this context. Passing parameters by reference is bad because it's actually not clear what is going on. Yes, there're less lines of code, but when you read this code you don't know whether variable changed as a side-effect of the function or not. Unclear side-effect make your code harder to understand. Commented Dec 4, 2014 at 16:56

2 Answers 2

4

You lose the reference inside the foreach. Change it to this and it'll work:

foreach( $array as $key => &$value ) {
Sign up to request clarification or add additional context in comments.

Comments

4

Your code does not modify the $array, it modifies $value.

There're couple of ways to get around that, one is foreach ($array as &$value), the other is modify $array[$key] inside the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.