Can't use method return value in write context

后端 未结 8 1327
野趣味
野趣味 2020-11-22 01:52

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()))
         


        
8条回答
  •  春和景丽
    2020-11-22 02:35

    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.

    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();
    }
    

提交回复
热议问题