php 不能在写上下文中使用方法返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1075534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't use method return value in write context
提问by Extrakun
I would think the following piece of code should work, but it doesn't (Edited: Now works in PHP 5.5+):
我认为下面的代码应该可以工作,但它没有(编辑:现在可以在 PHP 5.5+ 中使用):
if (!empty($r->getError()))
Where getError()is simply:
哪里getError()简单:
public function getError()
{
return $this->error;
}
Yet I end up with this error:
然而我最终遇到了这个错误:
can't use method return value in write context
不能在写上下文中使用方法返回值
What does this mean? Isn't this just a read?
这是什么意思?这不就是读书吗?
回答by Kornel
empty()needs to access the value by reference (in order to check whether that reference points to something that exists), and PHP before 5.5 didn't support references to temporary values returned from functions.
empty()需要通过引用访问值(为了检查该引用是否指向存在的东西),而 PHP 5.5 之前的版本不支持对从函数返回的临时值的引用。
However, the real problem you have is that you use empty()at all, mistakenly believing that "empty" value is any different from "false".
但是,您遇到的真正问题是您根本不使用empty(),错误地认为“空”值与“假”值有任何不同。
Empty is just an alias for !isset($thing) || !$thing. When the thing you're checking always exists (in PHP results of function calls always exist), the empty()function is nothing but a negation operator.
Empty 只是 的别名!isset($thing) || !$thing。当您要检查的东西始终存在时(在 PHP 中,函数调用的结果始终存在),该empty()函数只不过是一个否定运算符。
PHP doesn't have concept of emptyness. Values that evaluate to false are empty, values that evaluate to true are non-empty. It's the same thing. This code:
PHP没有空的概念。评估为 false 的值是空的,评估为 true 的值是非空的。这是同一件事。这段代码:
$x = something();
if (empty($x)) …
and this:
和这个:
$x = something();
if (!$x) …
has always the same result, in all cases, for all datatypes(because $xis defined empty()is redundant).
在所有情况下,对于所有数据类型都有相同的结果(因为$x定义empty()是多余的)。
Return value from the method always exists (even if you don't have returnstatement, return value exists and contains null). Therefore:
方法的返回值始终存在(即使您没有return语句,返回值也存在并包含null)。所以:
if (!empty($r->getError()))
is logically equivalent to:
在逻辑上等同于:
if ($r->getError())
回答by Peter Bailey
Note:This is a very high voted answer with a high visibility, but please note that it promotes bad, unnecessary coding practices! See @Kornel's answerfor the correct way.
Note #2:I endorse the suggestions to use @Kornel's answer. When I wrote this answer three years ago, I merely meant to explain the nature of the error, not necessarily endorse the alternative. The code snippet below is not recommended.
注意:这是一个投票率很高的答案,具有很高的知名度,但请注意,它会促进不良的、不必要的编码实践!有关正确方法,请参阅@Kornel 的答案。
注意#2:我赞同使用@Kornel's answer的建议。三年前我写这个答案时,我只是想解释错误的性质,不一定支持替代方案。不推荐使用下面的代码片段。
It's a limitation of empty()in PHP versions below 5.5.
这是5.5 以下 PHP 版本中empty()的限制。
Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).
注意:empty() 只检查变量,因为其他任何事情都会导致解析错误。换句话说,以下将不起作用:empty(trim($name))。
You'd have to change to this
你必须改成这个
// Not recommended, just illustrates the issue
$err = $r->getError();
if (!empty($err))
回答by George Claghorn
According to the PHP docs:
根据PHP 文档:
empty() only checks variables as anything else will result in a parse error
empty() 只检查变量,因为其他任何事情都会导致解析错误
You cannot use empty()directly on a function's return value. Instead, set the return from getError()to a variable and run empty()on the variable.
您不能empty()直接在函数的返回值上使用。相反,将 return fromgetError()设置为变量并empty()在变量上运行。
回答by Luke P M
I usually create a global function called is_empty() just to get around this issue
我通常会创建一个名为 is_empty() 的全局函数来解决这个问题
function is_empty($var)
{
return empty($var);
}
Then anywhere I would normally have used empty() I just use is_empty()
然后在我通常使用 empty() 的任何地方我只使用 is_empty()
回答by Jani Hartikainen
As pointed out by others, it's a (weird) limitation of empty().
正如其他人所指出的,这是 empty() 的(奇怪的)限制。
For most purproses, doing this is equal as calling empty, but this works:
对于大多数目的,这样做等同于调用空,但这是有效的:
if ($r->getError() != '')
回答by Jean Carlo Bambalan
The issue is this, you want to know if the error is not empty.
问题是这样的,您想知道错误是否为空。
public function getError() {
return $this->error;
}
Adding a method isErrorSet() will solve the problem.
添加方法 isErrorSet() 将解决问题。
public function isErrorSet() {
if (isset($this->error) && !empty($this->error)) {
return true;
} else {
return false;
}
}
Now this will work fine with this code with no notice.
现在这将在没有通知的情况下使用此代码正常工作。
if (!($x->isErrorSet())) {
echo $x->getError();
}
回答by quardas
The alternative way to check if an array is empty could be:
检查数组是否为空的另一种方法可能是:
count($array)>0
It works for me without that error
它对我有用,没有那个错误

