Hi
Anyone know what is up with callbacks? They seem to change results depending on type i.e. closure or method call:
[code=php]
class callbackProblems
{
public function __construct()
{
echo “<!DOCTYPE html>n”;
echo “<head>n<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>n”;
echo “</head>n”;
echo “<body>n”;
echo “<br>Simple test to see the difference in a callback being a closure, static and non-static<br>n”;
$c = “£”;
echo “We start with an annoying character: {$c} – the UK pound symbol<br>n”;
echo “<br>The preg_replace_callback function as a closure<br>n”;
$cWithCallback_closure = preg_replace_callback(‘/[^a-z]/’, function($c){
echo $c[0];
}, $c);
echo $cWithCallback_closure . “n”;
echo “<br>Now for the preg_replace_callback function as a static method call<br>n”;
$cWithCallback_static = preg_replace_callback(‘/[^a-z]/’, array(‘self’, ‘testStatic’), $c);
echo $cWithCallback_static . “n”;
echo “<br>Now for the preg_replace_callback function as a non-static method call<br>n”;
$cWithCallback_nonstatic = preg_replace_callback(‘/[^a-z]/’, array($this, ‘testNonStatic’), $c);
echo $cWithCallback_nonstatic . “n”;
echo “</body>n”;
echo “</html>n”;
die();
}
public static function testStatic($s)
{
echo “<pre>”, print_r($s), “</pre>”;
}
public function testNonStatic($s)
{
echo “<pre>”, print_r($s), “</pre>”;
}
}
$test = new callbackProblems;
I’m confused ?:eek:
Produces the following output:
[QUOTE]
Simple test to see the difference in a callback being a closure, static and non-static
We start with an annoying character: £ – the UK pound symbol
The preg_replace_callback function as a closure
£
Now for the preg_replace_callback function as a static method call
Array
(
[0] => �
)
1
Array
(
[0] => �
)
1
Now for the preg_replace_callback function as a non-static method call
Array
(
[0] => �
)
1
Array
(
[0] => �
)
1