Using the StaticFilterΒΆ

If it is inconvenient to load a given filter class and create an instance of the filter, you can use StaticFilter with it’s method execute() as an alternative invocation style. The first argument of this method is a data input value, that you would pass to the filter() method. The second argument is a string, which corresponds to the basename of the filter class, relative to the Zend\Filter namespace. The execute() method automatically loads the class, creates an instance, and applies the filter() method to the data input.

1
echo StaticFilter::execute('&', 'HtmlEntities');

You can also pass an array of constructor arguments, if they are needed for the filter class.

1
2
3
echo StaticFilter::execute('"',
                           'HtmlEntities',
                           array('quotestyle' => ENT_QUOTES));

The static usage can be convenient for invoking a filter ad hoc, but if you have the need to run a filter for multiple inputs, it’s more efficient to follow the first example above, creating an instance of the filter object and calling its filter() method.

Also, the FilterChain class allows you to instantiate and run multiple filter and validator classes on demand to process sets of input data. See FilterChain.

You can set and receive the FilterPluginManager for the StaticFilter to amend the standard filter classes.

1
2
3
4
5
$pluginManager = StaticFilter::getPluginManager()->setInvokableClass(
    'myNewFilter', 'MyCustom\Filter\MyNewFilter'
);

StaticFilter::setPluginManager(new MyFilterPluginManager());

This is useful when adding custom filters to be used by the StaticFilter.

Previous topic

Zend\Filter\Inflector

Next topic

Writing Filters

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Using the StaticFilter on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.