Wieso kann ich von Callback-Funktionen aus nicht
$this->foo beeinflussen/dessen Wert ändern?
Welchen Workarround gibt es?
$this->foo beeinflussen/dessen Wert ändern?
Welchen Workarround gibt es?
class prego
{
var $test;
function prego () { $this->test = "dies ist ein testtext"; }
function prego_callback () { /* tue was */ }
function prego_ruft_callback ()
{
# falsch
preg_replace_callback ("*** regex ***", "prego_callback", $this->test);
# falsch
preg_replace_callback ("*** regex ***", array ($this, "prego_callback"), $this->test);
# richtig, man beachte die Referenz ;o
preg_replace_callback ("*** regex ***", array (&$this, "prego_callback"), $this->test);
}
}
class tpl {
var $loops = array();
var $code = '';
function parse() {
$this->parse_blocks(&$this->code);
}
function parse_blocks(&$tpl) {
$tpl = preg_replace_callback('%-loop (.*)-(.*)-/loop \1-%sU', array(&$this, 'parse_block'), $tpl);
}
function parse_block($block) {
$loop = array('name' => $block[1], 'code' => $block[2]);
$this->parse_blocks($loop['code']);
while (isset($this->loops[$unique = uniqid('loop_')]));
$this->loops[$unique] = $loop;
return $unique;
}
}
$tpl = new tpl();
$tpl->code = 'hallo
-loop test1-
irgendwas
-loop test1_1-
nochwas
-/loop test1_1-
-/loop test1-
ende';
$tpl->parse();
echo '<pre>'.print_r($tpl, true).'</pre>';
Kommentar