Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zend.filter.callback.html

Size:13024
Storage flags:no_autoload,compress/gzip (27%)

Callback — Zend Framework 2 2.4.2 documentation

Callback

This filter allows you to use own methods in conjunction with Zend\Filter. You don’t have to create a new filter when you already have a method which does the job.

Supported Options

The following options are supported for Zend\Filter\Callback:

  • callback: This sets the callback which should be used.
  • callback_params: This property sets the options which are used when the callback is processed.

Basic Usage

The usage of this filter is quite simple. Let’s expect we want to create a filter which reverses a string.

1
2
3
4
$filter = new Zend\Filter\Callback('strrev');

print $filter->filter('Hello!');
// returns "!olleH"

As you can see it’s really simple to use a callback to define a own filter. It is also possible to use a method, which is defined within a class, by giving an array as callback.

1
2
3
4
5
6
7
8
9
// Our classdefinition
class MyClass
{
    public function Reverse($param);
}

// The filter definition
$filter = new Zend\Filter\Callback(array('MyClass', 'Reverse'));
print $filter->filter('Hello!');

To get the actual set callback use getCallback() and to set another callback use setCallback().

Note

Possible exceptions

You should note that defining a callback method which can not be called will raise an exception.

Default Parameters Within a Callback

It is also possible to define default parameters, which are given to the called method as array when the filter is executed. This array will be concatenated with the value which will be filtered.

1
2
3
4
5
6
7
$filter = new Zend\Filter\Callback(
    array(
        'callback' => 'MyMethod',
        'options'  => array('key' => 'param1', 'key2' => 'param2')
    )
);
$filter->filter(array('value' => 'Hello'));

When you would call the above method definition manually it would look like this:

1
$value = MyMethod('Hello', 'param1', 'param2');

Table Of Contents

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 Callback 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.

For more information about the PHK package format: http://phk.tekwire.net