PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Using the static staticFilter() method

If it is inconvenient to load a given filter class and create an instance of the filter, you can use the static method Zend_Filter::filterStatic() 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 staticFilter() method automatically loads the class, creates an instance, and applies the filter() method to the data input.

<?php
echo Zend_Filter::filterStatic('&''HtmlEntities');

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

<?php
echo Zend_Filter::filterStatic('"',
                               
'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 Zend_Filter_Input class allows you to instantiate and run multiple filter and validator classes on demand to process sets of input data. See Zend_Filter_Input.

Namespaces

When working with self defined filters you can give a fourth parameter to Zend_Filter::filterStatic() which is the namespace where your filter can be found.

<?php
echo Zend_Filter::filterStatic(
    
'"',
    
'MyFilter',
    array(
$parameters),
    array(
'FirstNamespace''SecondNamespace')
);

Zend_Filter allows also to set namespaces as default. This means that you can set them once in your bootstrap and have not to give them again for each call of Zend_Filter::filterStatic(). The following code snippet is identical to the above one.

<?php
Zend_Filter
::setDefaultNamespaces(array('FirstNamespace''SecondNamespace'));
echo 
Zend_Filter::filterStatic('"''MyFilter', array($parameters));
echo 
Zend_Filter::filterStatic('"''OtherFilter', array($parameters));

For your convenience there are following methods which allow the handling of namespaces:

  • Zend_Filter::getDefaultNamespaces(): Returns all set default namespaces as array.

  • Zend_Filter::setDefaultNamespaces(): Sets new default namespaces and overrides any previous set. It accepts either a string for a single namespace of an array for multiple namespaces.

  • Zend_Filter::addDefaultNamespaces(): Adds additional namespaces to already set ones. It accepts either a string for a single namespace of an array for multiple namespaces.

  • Zend_Filter::hasDefaultNamespaces(): Returns TRUE when one or more default namespaces are set, and FALSE when no default namespaces are set.

Zend Framework