|
Posted by jody.florian on 09/30/17 11:37
index.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", "bob@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/index.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.
I'm still quite at a loss myself. I'm hazy on my lambda calculus but
surely there's a way round this?
--
J Florian
Navigation:
[Reply to this message]
|