1

Hi I've been playing around with phps filter class getting and have come across a snag with the filter_callback filter.

the following rough bit of code works but shows an error every time

Warning: filter_var() [function.filter-var]: First argument is expected to be a valid callback in /Users/Rob/sites/test_val.php on line 12

class test

{

public function callback($string)
{

$var = filter_var($string, FILTER_CALLBACK, array('options' => $this->foo($string)));

} 

public function foo($string){

echo $string;


}

}


$test = new test();

$string = 'test';

$tested = $test->callback($string);

am i calling the function correctly or is there a different way?

2

3 Answers 3

10
$this->foo($string)

...should be...

array($this, 'foo')

When using a method as a callback, you need to provide the reference in this manner.

Documentation.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your help, is their a way to pass multiple variables to the function callback?
@Robert The function uses the callback, not you, so you can't pass additional arguments.
3

This code works for me :)

 <?php
    class myClass {
     public function myFunc($var){
       return filter_var($var, FILTER_CALLBACK, array('options'=> 'self::myCallback'));
     }
     public function myCallback(){
      return true;
     }
    }
$obj = new myClass();

var_dump($obj->myFunc("[email protected]"));
//output:- bool(true)

?>

Comments

0
echo filter_var('wo9w9w9', FILTER_CALLBACK, array('options' => array(new MyFilter(), 'filter1'))) . PHP_EOL;  

options You can directly transfer object instance, like $this or new self ,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.