Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use method return value in write context

Tags:

php

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?

like image 611
Extrakun Avatar asked Jul 02 '09 16:07

Extrakun


4 Answers

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())
like image 120
Kornel Avatar answered Nov 07 '22 05:11

Kornel


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 answer for 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.


It's a limitation of empty() in PHP versions below 5.5.

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

You'd have to change to this

// Not recommended, just illustrates the issue
$err = $r->getError();
if (!empty($err))
like image 30
Peter Bailey Avatar answered Nov 07 '22 04:11

Peter Bailey


Prior to PHP 5.5, the the PHP docs used to say:

empty() only checks variables as anything else will result in a parse error

In PHP < 5.5 you weren't able use empty() directly on a function's return value. Instead, you could assign the return from getError() to a variable and run empty() on the variable.

In PHP >= 5.5 this is no longer necessary.

like image 38
Toress Avatar answered Nov 07 '22 04:11

Toress


I usually create a global function called is_empty() just to get around this issue

function is_empty($var)
{ 
 return empty($var);
}

Then anywhere I would normally have used empty() I just use is_empty()

like image 19
Luke P M Avatar answered Nov 07 '22 05:11

Luke P M