Re: Re: Problem with passing function return values by reference
| From: | Chuck Hagenbuch | Date: | Fri, 17 Nov 2000 16:19:21 +0000 |
| Subject: | Re: Re: Problem with passing function return values by reference | ||
| References: | 1 2 3 4 5 6 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-38425@lists.php.net to get a copy of this message | ||
Quoting Andi Gutmans <andi@zend.com>:
> Yeah, I know what the error is. It is supposed to limit people to only send
> real variables by reference.
> When/if you have time please send more information.
Well, here's one short example. If I have a function mayFail():
function mayFail($parameters) {
$somethingWorked = trySomething();
if ($somethingWorked) {
return true;
} else {
return new PEAR_Error("something failed");
}
}
... and then I want to check the return value, so that I can act on whether or
not it failed:
if (PEAR::isError(mayFail("foo"))) {
tellUser("oops, it didn't work");
}
... then currently you'll get an error (because PEAR::isError() takes a
reference, presumably to avoid copying objects unnecessarily). There are two
workarounds that I've found; one is to assign the return value of mayFail() to
a variable before sending it through isError() - an unnecessary step. The other
is to return a dummy object instead of true from mayFail(). Neither of those is
really clean or desireable, though.
-chuck
--
Charles Hagenbuch, <chuck@horde.org>
"If you can't stand the heat, get out of the chicken!" - Baby Blues