|
Posted by jody.florian on 09/27/32 11:37
example.php:
<?php
class myclass {
protected $vars = array();
function addVar($name,$val){ $this->vars[$name] = $val; }
function output(){
$fileContents = file_get_contents("my.template");
$outputString = preg_replace_callback(
'@{([A-Z]+)}@',
create_function('$matcharray','return
$this->vars[$matches[1]];'),
$fileContents);
echo $outputString;
} }
$obj = new myclass();
$obj->addVar("NAME", "bob");
$obj->addVar("MOBILE", "0207 PHP RULES");
$obj->addVar("EMAIL", "b...@bob.com");
$obj->output();
?>
my.template :
<ul>
<li>{NAME}</li>
<li>{MOBILE}</li>
<li>{EMAIL}</li>
</ul>
It produces the error:
Fatal error: Using $this when not in object context in
/www/thisproblem/example.php(10) : runtime-created function on line 1
In the example at
http://uk.php.net/manual/en/function.preg-replace-callback.php
They're not implementing a callback function with create_function() in
a class context, as opposed to my example above.
Since it's a) not wise and b) not possible to declare a function within
a method (since it would be re-declared each time the method's used)
I'm still quite at a loss myself
I'm hazy on my lambda calculus but surely there's a way round this,
other than creating a method solely as a one-off callback?
Cheers
--
J Florian
Navigation:
[Reply to this message]
|