这是一个错误还是我错了?
/**
* Returns nodes found by xpath expression
*
* @param string $xpath
* @return array
*/
public function getXpath($xpath)
{
if (empty($this->_xml)) {
return false;
}
if (!$result = @$this->_xml->xpath($xpath)) {
return false;
}
return $result;
}
此代码取自 Magento。您可以在其公共 svn 中查看特定文件:
http: //svn.magentocommerce.com/source/branches/1.5/lib/Varien/Simplexml/Config.php
现在我认为 (!$result = @$this->_xml->xpath ($xpath))
永远不可能计算为 true
,因此 return false
语句永远不会发生。
因为xpath的返回值的赋值,无论对变量$result
是true还是false,总是返回true,取反总是返回false
。
所以这是一个错误或完全冗余的代码,还是我错了?
仅供参考:我目前正在调试一个问题,其中一些配置元素“丢失”,我认为错误就在那里。
/**
* Returns nodes found by xpath expression
*
* @param string $xpath
* @return array
*/
public function getXpath($xpath)
{
if (empty($this->_xml)) {
return false;
}
if (!$result = @$this->_xml->xpath($xpath)) {
return false;
}
return $result;
}
This code is taken from Magento. You can checkout the specific file in their public svn:
http://svn.magentocommerce.com/source/branches/1.5/lib/Varien/Simplexml/Config.php
Now I think that (!$result = @$this->_xml->xpath($xpath))
can never ever evaluate to true
and thus the return false
statement can never happen.
Because the assignment of the return value of xpath, regardless of whether it is true or false to the variable $result
always returns true and negated always returns false
.
So this is a bug or a completely redundant code, or am i wrong?
FYI: I am currently debugging a problem where some config elements get "lost" and I assume the error is somewhere in there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是建立这样的条件的一种奇怪的方式,但我认为这是有道理的。
$result =
会将 xpath 操作的结果分配给$result
。如果结果为
false
(值xpath( )
将在出现错误时返回),条件将匹配并且函数将返回 false。This is a weird way of building a condition like that, but I think it makes sense.
$result =
will assign the result of the xpath operation to$result
.If that result is
false
(a value whichxpath()
will return in case of an error), the condition will match and the function will return false.“因为将
xpath
的返回值赋值给变量$result
总是返回true”这个说法是错误的,我不知道你为什么得出这个结论。赋值运算符的值始终等于分配的值,因此它很容易为 false 或 true (($x = false) == false
)"Because the assignment of the return value of
xpath
to the variable$result
always returns true" this statement is wrong and I have no idea why did you make this conclusion. Value of assignment operator is always equal to value that was assigned, so it can easily be false or true (($x = false) == false
)您可以使用以下命令进行测试,
因此如果
SimpleXml::xpath
返回空数组或FALSE
,Magento 函数将返回FALSE
。对数组取反将首先将其类型转换为布尔值。 空数组类型转换为 Boolean 将为 FALSE< /a>.这意味着,当您的 XPath 在语法上是正确的,但没有找到任何结果时,Magento 将返回FALSE
。You can test that with
So if
SimpleXml::xpath
returns an empty array orFALSE
, the Magento function will returnFALSE
. Negating an array will typecast it to boolean first. Empty Arrays typecasted to Boolean will be FALSE. Which means, when your XPath is syntactically correct, but did not find any results, Magento will returnFALSE
.我发现将内部分配括起来会很有帮助:
从视觉上看,分配现在是一个子任务,必须在反转之前执行。如果没有额外的括号,它的执行完全相同,这只是有助于您自己的代码阅读。
任何赋值命令都会返回所返回的值。这可以让您执行以下操作:
$e
的新值将返回到$d
,而$d
将返回到$c
,并且很快...I find it helps to parenthesise the inner assignment like this:
Visually the assignment is now a sub-task and must be executed before being inverted. Without the extra parentheses it performs exactly the same, this just helps your own code reading.
Any assignment command returns the value being returned. This lets you do things like:
The new value of
$e
is being returned to$d
, which is being returned to$c
, and so on...