鉴于以下示例,任何人都可以推荐一种最佳做法,即在不扩展 Mustache 类的情况下从模板访问 $string 或 HelloWorld::getString() 吗?
<?php
Class HelloWorld{
protected $string;
function __construct(){
$this->string = 'Hello World';
}
function setString($str) { $this->string = $str; }
function getString() { return $this->string; }
}
# here goes nothing
$h = new HelloWorld();
$m = new Mustache();
echo $m->render('{{string}}', $h);
?>
正如你想象的那样,如果我公开 $string,它会按预期工作。我错过了什么?