PHP Classes

File: FILTERS.md

Recommend this page to a friend!
  Classes of Johnny Mast   PHP Filters and Actions   FILTERS.md   Download  
File: FILTERS.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: PHP Filters and Actions
Listen to events and execute registered actions
Author: By
Last change:
Date: 6 years ago
Size: 1,746 bytes
 

Contents

Class file image Download

Filters callback functions

function func_prepend_at($text='') {
    return '@@'.$text;
}
Sandbox\Filters::add_filter('prepend_at', 'func_prepend_at');

$out = Sandbox\Filters::apply_filter('prepend_at', 'This is a text');
echo "Output: ".$out."\n";

*Output*

$ php ./functions.php
Output: @@This is a text

Filters with closures

Sandbox\Filters::add_filter('prepend_at', function($text='') {
    return '@@'.$text;
});

$out = Sandbox\Filters::apply_filter('prepend_at', 'This is a text');
echo "Output: ".$out."\n";

*Output*

$ php ./functions.php
Output: @@This is a text

Filters with priority

function func_second($text='') {
    return '@@'.$text;
}
Sandbox\Filters::add_filter('prepend_at', 'func_second',1);

function func_first($text='') {
    return '!!'.$text;
}
Sandbox\Filters::add_filter('prepend_at', 'func_first', 0);

$out = Sandbox\Filters::apply_filter('prepend_at', 'This is a text');
echo "Output: ".$out."\n";

*Output*

$ php ./functions.php
Output: @@!!This is a text

Filters in classes

class Filter {

    public function prepend_chars($text='') {
        return '@@'.$text;
    }

    public function append_chars($text='') {
        return $text.'@@';
    }

    public function execute() {

        Sandbox\Filters::apply_filter('manipulate_string', [$this, 'prepend_chars']);
        Sandbox\Filters::apply_filter('manipulate_string', [$this, 'append_chars']);

        return Sandbox\Filters::apple_filter('manipulate_string', 'This is a text');
    }
}

$instance = new Filter;
$out = $instance->execute();

echo "Output: ".$out."\n";

*Output*

$ php ./functions.php
Output: @@This is a text@@