程序go 2009-07-02 16:58 采纳率: 100%
浏览 135
已采纳

不能在写上下文中使用方法返回值

I would think the following piece of code should work, but it doesn't (Edited: Now works in PHP 5.5+):

if (!empty($r->getError()))

Where getError() is simply:

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?

转载于:https://stackoverflow.com/questions/1075534/cant-use-method-return-value-in-write-context

  • 写回答

7条回答 默认 最新

  • Lotus@ 2010-12-01 19:05
    关注

    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.

    However, the real problem you have is that you use empty() at all, mistakenly believing that "empty" value is any different from "false".

    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.

    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:

    $x = something();
    if (empty($x)) …
    

    and this:

    $x = something();
    if (!$x) …
    

    has always the same result, in all cases, for all datatypes (because $x is defined empty() is redundant).

    Return value from the method always exists (even if you don't have return statement, return value exists and contains null). Therefore:

    if (!empty($r->getError()))
    

    is logically equivalent to:

    if ($r->getError())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用