现在,在跳转之前,您应该如何不应混合范围:我意识到这一点。但是,这是一种情况,即必须发生范围混合或必须发生主要代码重复 - 周围没有任何东西。我更喜欢范围混合。在PHP中导出变量到以前的范围
这么说,我想这样的代码:
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'));
通话
然后,在模板/ NotFound.php,你会碰到这样的:
<html>
<body>
<?php echo $e_requested_url; ?> could not be found. Did you mean <?php echo $e_misspelling: ?>?
</body>
</html>
我们真的不想让我们的模板作者做更多的事情......虽然如果没有更好的可用的话,可以完成。
保留变量名$ a,$ b,$ c是至关重要的。好吧。 – 2010-08-22 00:54:13
+1你先知道了。您没有声明$ a,$ band $ c作为班级成员? – NAVEED 2010-08-22 00:54:43
@NAVEED不需要。 PHP可以在运行时添加类成员 – NullUserException 2010-08-22 00:58:02