PHP eval() 函数
PHP 函数 eval() 似乎是一个相当有趣的函数。有人可以解释为什么它在这种给定情况下有效,在线:14
function Parse($inFrontEnd)
{
// Now create an array holding translation tokens with some from above
// Load translation table into buffer
$tableLines = file(Utilities::GetRelativePath(TTABLE_DIR).TTABLE); // Array of lines from TTable.cfg
// Explode by whitespace
foreach($tableLines as $aLine)
{
$lineParts = EXPLODE(' ', $aLine);
$word = "/".$lineParts[0]."/";
$definition = $lineParts[1];
// Add key (word) => value (definition) to array
// Eval() to return value of the const
Main::$translateChars[$word] = eval("return $definition;");
}
// Read data from template file
$parseArray = file($inFrontEnd); // Load FrontEnd source code into array ready for parse
/* Perform the translation of template by the translation table defined data */
$parseArray = preg_replace(array_keys(Main::$translateChars), array_values(Main::$translateChars), $parseArray);
return $parseArray;
}
所以我在这里所做的是从模板目录中读取模板。 templatename.php 文件由类似常量的文本标记组成,然后通过正则表达式进行翻译,将标记替换为常量及其名称所包含的数据,从而返回完全验证的网页页面,为用户打印该页面来查看。通过允许在许多网页(模板)上重复使用这些令牌,这使得页面变得非常动态。
我的问题是:我在使用 eval() 的行上遇到了一段时间的麻烦。我想要做的是填充一个数组,其中每个键都是从我命名的翻译表(TTable.cfg)中读取的常量的名称,该表保存每个标记的名称和常量与其关联:
TITLE TITLE
CSS_INCLUDE CSS_INCLUDE
SHOW_ALL_POSTS SHOW_ALL_POSTS
...
因此,使用协议 [TOKEN] [CONSTANT][CR][LF]
数组中的键可以很好地创建,但是当我将键与以下项关联时,这些值将返回 null 或破坏我的代码:constant( $定义); 它抱怨找不到声明的常量。但是,当我在这一行上按原样使用 eval 时,每个键与以下项关联: eval("return $definition;"); 它按照我想要的方式工作 - 值作为相应常量的数据。
对于这篇文章的长度,我深表歉意。除了我发现的案例之外,我找不到任何其他例子来回答我的问题。
The PHP function eval() appears to be a rather interesting function. Can someone explain why it works in this given situaton, on line: 14
function Parse($inFrontEnd)
{
// Now create an array holding translation tokens with some from above
// Load translation table into buffer
$tableLines = file(Utilities::GetRelativePath(TTABLE_DIR).TTABLE); // Array of lines from TTable.cfg
// Explode by whitespace
foreach($tableLines as $aLine)
{
$lineParts = EXPLODE(' ', $aLine);
$word = "/".$lineParts[0]."/";
$definition = $lineParts[1];
// Add key (word) => value (definition) to array
// Eval() to return value of the const
Main::$translateChars[$word] = eval("return $definition;");
}
// Read data from template file
$parseArray = file($inFrontEnd); // Load FrontEnd source code into array ready for parse
/* Perform the translation of template by the translation table defined data */
$parseArray = preg_replace(array_keys(Main::$translateChars), array_values(Main::$translateChars), $parseArray);
return $parseArray;
}
So what I'm doing here is reading in a template from a template directory. The templatename.php file comprises of text tokens written constant-like, which are then translated by regular expressions replacing the tokens with the data the constants with their names hold, thus returning a fully validated page of a webpage, which is printed for the user to view. This allows pages to be very dynamic by allowing the reuse of these tokens over many webpages (templates).
My question is: I had trouble for a while with the line that uses eval(). What I'm trying to do there is fill an array with each key being the name of the constant read in from, what I've named, the translation table (TTable.cfg), which holds the name of each token and the constant associated with it:
TITLE TITLE
CSS_INCLUDE CSS_INCLUDE
SHOW_ALL_POSTS SHOW_ALL_POSTS
...
So with the protocol [TOKEN] [CONSTANT][CR][LF]
The keys within the array would be created fine, but the values would return null or break my code when I had the key be associated with: constant($definition);
It complained it couldn't find the constants being declared. However, when I use eval as is on this line, each key associated with: eval("return $definition;");
it works as I want it - the values as their corresponding constant's data.
I do apologise for the length of this post. I couldn't find any other example for my question other than the case I found it in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您使用双引号时,变量
$definition
的值将替换为字符串定义"return $definition;"
。因此,传递给 eval 的内容至关重要,如下所示:"return FOO;"
,其中FOO
是变量 $definition 的值。现在,使用
eval()
计算该代码,并将结果作为计算结果返回,即常量FOO
的值(每次迭代中都不同) )。在这种情况下使用常量更有意义:它更快、更安全且更具可读性。
这也将使您了解为什么它在使用
eval()
而不仅仅是constant()
时有效; PHP 将未知常量替换为它们各自的名称,因此 eval 适用于您的情况。然而,任何其他方式都会引发警告并且是一种不好的做法,因为它无法让读者清楚地了解发生了什么。The value of the variable
$definition
is replaced into the string definition"return $definition;"
, as you're using double quotes. Hence, what is passed to eval is essential something like this:"return FOO;"
, whereFOO
is the value of the variable $definition.Now, that code is evaluated using
eval()
, and the result is returned as the result of the evaluation, which is the value of the constantFOO
(different in each iteration).Using the
constant
in this case makes more sense: It is faster, potentially securer, and more readable.This will also give you some insight of why it works when using
eval()
and not justconstant()
; PHP replaces unknown constants with their respective names, thus eval works in your case. However, any other way would raise warnings and be a bad practice, as it doesn't make clear what's going on to the reader.请记住,
eval
是邪恶的,所以当你可以避免它时就不要使用它。在这里你可以只使用constant
代替。Remember,
eval
is evil, so don't use it, when you can avoid it. Here you could just useconstant
instead.最微小的疏忽。在从翻译表的每一行中分解 $definition 后,我忘记清理它。所以一个简单的trim()就解决了这个问题。
现在可以了。
疯狂:D
The tiniest of oversights. I forgot to clean $definition after I exploded it from each line of the translation table. So a simple trim() has solved the problem.
Now works.
Crazy :D