berkes
berkes

Reputation: 27563

Array_filter in the context of an object, with private callback

I want to filter an array, using the array_filter function. It hints at using call_user_func under water, but does not mention anything about how to use within the context of a class/object.

Some pseudocode to explain my goal:

class RelatedSearchBlock {
  //...
  private function get_filtered_docs() {
    return array_filter($this->get_docs(), 'filter_item');
  }

  private filter_item() {
    return ($doc->somevalue == 123)
  }
}

Would I need to change 'filter_item' into array($this, 'filter_item') ? Is what I want possible at all?

Upvotes: 23

Views: 20400

Answers (1)

deceze
deceze

Reputation: 522135

Yes:

return array_filter($this->get_docs(), array($this, 'filter_item'));

See the documentation for the callback type.

Upvotes: 69

Related Questions