php PHP致命错误无法在写入上下文中使用方法返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12129786/
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
PHP fatal error Can't use method return value in write context
提问by ro ko
I have seen this question being asked many times, but I am yet confused and came across something similar condition. I am not sure if thisand my context are same or not, as long as I am convinced this is a different scenario or else I might have misunderstood the explanation. O.k. here is my scenario:
我已经多次看到这个问题被问到,但我仍然感到困惑并遇到了类似的情况。我不确定这和我的上下文是否相同,只要我确信这是一个不同的场景,否则我可能误解了解释。好的,这是我的场景:
$amount = isset($cost->getCostAmount()) ? $cost->getCostAmount() : 0;
Function costAmount() is dynamically added during run-time and it may or may not exist. So I need to first check if my function exists or not and rest is pretty clear. But now in this case I get a fatal error:
函数 costAmount() 在运行时动态添加,它可能存在也可能不存在。所以我需要首先检查我的函数是否存在,其余的很清楚。但现在在这种情况下,我收到一个致命错误:
Fatal error: Can't use method return value in write context in ..../file.php
致命错误:无法在 ..../file.php 的写入上下文中使用方法返回值
Now if I do something like this:
现在,如果我做这样的事情:
$amount = $cost->getCostAmount() ? $cost->getCostAmount() : 0;
Obviously I would get an error:
显然我会得到一个错误:
Call to undefined method: getCostAmount
调用未定义的方法:getCostAmount
if the function doesn't exist. What could be a possible solution for this? Explanation will be considered helpful.
如果函数不存在。对此有什么可能的解决方案?解释将被认为是有帮助的。
Request: Please add an adequate comment to why the question has been downvoted so that I would be able to improve my questions, in the future.
请求:请对问题被否决的原因添加足够的评论,以便我将来能够改进我的问题。
回答by Peter
change this:
改变这个:
$amount = isset($cost->getCostAmount()) ? $cost->getCostAmount() : 0;
to..
到..
$amount = method_exists($cost, 'getCostAmount') ? $cost->getCostAmount() : 0;
because this piece of code isset($cost->getCostAmount())is executing method getCostAmounteven if it doesn't exist
因为这段代码即使不存在isset($cost->getCostAmount())也在执行方法getCostAmount
回答by Shiplu Mokaddim
issetrequires you pass a variable to it not a function. It can not check whether the return value is set.
isset要求您将变量传递给它而不是函数。它无法检查是否设置了返回值。
You should use it like this,
你应该像这样使用它,
$cost_amount = $cost->getCostAmount();
$amount = isset($cost_amount) ? $cost_amount : 0;
Even this code does not make sense. Because here $cost_amount will be always set. If getCostAmountreturns nullor emptystring you should check it that way.
甚至这段代码也没有意义。因为这里总是设置 $cost_amount。如果getCostAmount返回null或empty字符串,您应该以这种方式检查它。
$cost_amount = $cost->getCostAmount();
$amount = !is_null($cost_amount) ? $cost_amount : 0;
Also your code does not find getCostAmountfunction. If you know this is declared somewhere include it. If this method is generated dynamically you can check by using method_exists.
此外,您的代码没有找到getCostAmount功能。如果您知道这是在某处声明的,请将其包含在内。如果此方法是动态生成的,您可以使用method_exists.
$amount = method_exists($cost, 'getCostAmount')? $cost->getCostAmount(): 0;
回答by tomsv
Try changing isset() to function_exists()
尝试将 isset() 更改为 function_exists()
回答by Romain
You can use: method_exists(http://www.php.net/manual/en/function.method-exists.php) in the following way:
你可以使用:method_exists(http://www.php.net/manual/en/function.method-exists.php)以下列方式:
var_dump(method_exists($cost,'getCostAmount'));

