Re: Re: Problem with passing function return values by reference

From: Date: Fri, 17 Nov 2000 16:29:43 +0000
Subject: Re: Re: Problem with passing function return values by reference
References: 1 2 3 4 5 6 7  Groups: php.dev 
Request: Send a blank email to php-dev+get-38431@lists.php.net to get a copy of this message
Well the whole idea of a function saying it should get it's argument by reference is that it *expects* you to give it something that can be sent by reference. I understand the concern of people who have the following problem: function &mayFail() {
        return new foo;
} and it doesn't work for them. But actually your case will probably not be allowed even if the previous case gets fixed. Andi At 11:19 AM 11/17/00 -0500, Chuck Hagenbuch wrote:
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 -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net For additional commands, e-mail: php-dev-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net
--- Andi Gutmans <andi@zend.com> http://www.zend.com/

« previous php.dev (#38431) next »