0

Is it possible to rewrite this method to use yield?

The method is traversing through a multidimensional array and edit each value (strip invalid UTF8 chars)

public function strip_invalid_utf8($input){
    if(is_array($input)){
        foreach($input as &$value){
            $value = $this->strip_invalid_utf8($value);
        }

        return $input;
    }
    else{
        return filter_utf8($input);
    }
}
clarkk
  • 27,151
  • 72
  • 200
  • 340

1 Answers1

0

I'd like to say no.

Based on an excerpt from this great answer:

The heart of a generator function is the yield keyword. In its simplest form, a yield statement looks much like a return statement, except that instead of stopping execution of the function and returning, yield instead provides a value to the code looping over the generator and pauses execution of the generator function.

Unless you need to carry on execution of the function whilst being able to output values, I'd say you don't need to convert to a Generator Function.

treyBake
  • 6,440
  • 6
  • 26
  • 57