I’m wondering is it possible somehow?
sample code
class CustomFilter {
...
function FilterData($data, $function_name) {
# here I want to execute that function against $data argument
}
...
}
function add2($x) {
return $x + 2;
}
$fd = new CustomFilter;
$fd->FilterData(5, ‘add2’);
I can pass function name and execute it using eval() outside of class, but inside the class that function is not visible. Is it any way to make user-defined function work inside the class?
What’s the idea?
Using class constructor user passes configuration array into class. Part of array may contain different filter parameters, example:
config array
$cfg = array(
'image' => array(
'params' => array(
'+src' => 'string[filter:url]',
'maxwidth' => 'integer[filter:1+]',
'byte' => 'integer[filter:0-255]',
'alt' => 'string',
'align' => 'string[values:left|right|center]',
'binary' => false
),
...
),
...
);
Filter parameters define filter criteria for some input fields. In this example ‘string[filter:url]‘ calls some inner method to check if parameter ‘src’ is valid url parameter, ‘integer[filter:1+]‘ checks if value is integer greater than 1, ‘false‘ means “don’t filter value”. But one possible value is ‘string[customfilter:myfunction]‘ and that means “check argument with user-defined function myfunction”.
I need to set local function name as string and execute it inside the class.
Any suggestions? Thanks in advance.
You can call it like this:
Inside your
dataFilterfunction.Example:
Result: