这是一个错误还是我错了?

发布于 2024-10-19 13:38:27 字数 889 浏览 4 评论 0原文

/**
 * 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

囍孤女 2024-10-26 13:38:27

这是建立这样的条件的一种奇怪的方式,但我认为这是有道理的。

$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 which xpath() will return in case of an error), the condition will match and the function will return false.

幸福%小乖 2024-10-26 13:38:27

“因为将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 )

吐个泡泡 2024-10-26 13:38:27

您可以使用以下命令进行测试,

var_dump(!$result = FALSE);    // TRUE
var_dump(!$result = array());  // TRUE
var_dump(!$result = array(1)); // FALSE

因此如果 SimpleXml::xpath 返回空数组或 FALSE,Magento 函数将返回 FALSE。对数组取反将首先将其类型转换为布尔值。 空数组类型转换为 Boolean 将为 FALSE< /a>.这意味着,当您的 XPath 在语法上是正确的,但没有找到任何结果时,Magento 将返回 FALSE

You can test that with

var_dump(!$result = FALSE);    // TRUE
var_dump(!$result = array());  // TRUE
var_dump(!$result = array(1)); // FALSE

So if SimpleXml::xpath returns an empty array or FALSE, the Magento function will return FALSE. 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 return FALSE.

菩提树下叶撕阳。 2024-10-26 13:38:27

我发现将内部分配括起来会很有帮助:

if (! ($result = @$this->_xml->xpath($xpath)) ) {

从视觉上看,分配现在是一个子任务,必须在反转之前执行。如果没有额外的括号,它的执行完全相同,这只是有助于您自己的代码阅读。

任何赋值命令都会返回所返回的值。这可以让您执行以下操作:

$a = $b = $c = $d = $e = 'A made-up value';

$e 的新值将返回到 $d,而 $d 将返回到 $c,并且很快...

I find it helps to parenthesise the inner assignment like this:

if (! ($result = @$this->_xml->xpath($xpath)) ) {

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:

$a = $b = $c = $d = $e = 'A made-up value';

The new value of $e is being returned to $d, which is being returned to $c, and so on...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文