我使用自制的 MVC 系统,其中视图通过在方法的上下文中访问模型,因此能够访问$this
.
动态包含的视图示例:
...
<div>
Hello <?= $this->user->name ?>
</div>
...
现在,我有一些代码要分解为函数,并带有一些额外的参数。例如 :
function colored_hello($color) {
?>
<div style="background-color:<?= $color ?>">
Hello <?= $this->user->name ?>
</div>
<?
}
问题是我无权访问$this
,因为该函数不是方法。但我不想用演示文稿破坏我的模型或控制器。
Hance,我希望能够动态调用这个函数,作为一种方法。像面向方面的编程:
# In the top view
magic_method_caller("colored_hello", $this, "blue")
可能吗 ?或者你有没有更好的方法?