开发者

PHP FILTER_CALLBACK in OOP Class

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($strin开发者_JAVA技巧g);

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


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


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("myname@gmail.com"));
//output:- bool(true)

?>


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 ,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜