- preg_replace_callback: documentation ( source)
<?php
class ok {
public function t() {
$this->replacer( '*' );
return preg_replace_callback( '/(t)/', array( $this, 'replacerx'), 'this is a simple test' );
}
private function replacer( $input ) {
static $cache = '-';
if ( ! is_array( $input ) ) {
$cache = $input;
} else {
return $cache . $input[1] . $cache;
}
}
}
class buggyphp extends ok {}
class buggyphptoo extends ok {
public function t() {
return parent::t();
}
}
$buggy = new buggyphp();
echo 'Result buggyphp: ' . $buggy->t();
echo "\n";
$buggy = new buggyphptoo();
echo 'Result buggyphptoo: ' . $buggy->t();
echo "\n";
$a = new ok();
echo 'Result ok: ' . $a->t();