3

if want to do this following:

    $filteredValues = array_filter($rawValues, function($rawValue) {
        return $this->validateValue($rawValue);
    });

validateValue is a private method in the same class.

How can i use $this context in array_filter in this way?

Marty Wallace
  • 34,046
  • 53
  • 137
  • 200

1 Answers1

4

If you use PHP 5.3, PHP does not recognize $this as closure, you need to do a trick like JavaScript:

$self = $this;
$filteredValues = array_filter($rawValues, function($rawValue) use ($self) {
    return $self->validateValue($rawValue);
});

Note that the above will only give you access to the object through the public API of the object. This is different from the 5.4 support for Closure rebinding which allows full access to $this.

cuongle
  • 74,024
  • 28
  • 151
  • 206