【发布时间】:2010-08-22 00:46:10
【问题描述】:
现在,在您开始讨论不应该如何混合范围之前:我意识到这一点。但是,在这种情况下,要么必须发生范围混合,要么必须发生主要的代码重复——周围没有任何东西。而且我更喜欢范围混合。
也就是说,我想要这个代码:
function a() {
$a = "a";
$b = "b";
$c = "c";
}
function b() {
a();
echo $a . $b . $c;
}
b(); // Output: abc
echo $a; // Should raise a notice that $a is undefined
能够按照评论工作。我知道这在大多数语言中是不可能的——不过我可以在 Ruby 中做到这一点;并想知道你是否可以用 PHP 做到这一点。
在实际情况下,变量的名称是事先不知道的。
同样,这是代码重复或这个——绝对没有办法解决。
另外,如果 a 必须是 a('b') 之类的东西,那也没关系。
实际上,代码是这样的:
static function renderError($what, $vararray) {
foreach($vararray as $key => $val) { /* this foreach is the code we want to decouple */
$key = 'e_'.$key;
$$key = htmlspecialchars($val);
}
ob_clean();
exit(eval('?>'.file_get_contents(ROOT."/templates/$what.php")));
}
像E_Render::renderError('NotFound', array( 'requested_url' => '/notfound', 'misspelling' => '/reallynotfound' ));这样的电话
然后,在 templates/NotFound.php 中,您将有类似的内容:
<html>
<body>
<?php echo $e_requested_url; ?> could not be found. Did you mean <?php echo $e_misspelling: ?>?
</body>
</html>
我们真的不想让我们的模板作者做更多的事情......尽管如果没有更好的可用,$e['requested_url'] 可以做。
【问题讨论】: