Re: [RFC] Allow non-variable arguments to empty() and isset()

From: Date: Thu, 03 May 2012 20:42:09 +0000
Subject: Re: [RFC] Allow non-variable arguments to empty() and isset()
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-60439@lists.php.net to get a copy of this message
Patrick ALLAERT wrote:
2012/5/3 Lester Caine<lester@lsces.co.uk>:
Anthony Ferrara wrote:
I voted for the ability to use an expression for isset() as well, since I agree with Ferenc, it's a matter of consistency. Sure, the use-case for isset() is definitely weaker than for empty(), but at the same token they are definitely related...
I just can't help feeling that it is the wrong use of both. If the function is returning a value, then it's returning a value that needs to be used somewhere so the work flow handles that. If the function returns nothing instead that just seems wrong and needs to be handled better. I'm used to getting back 'false' if the function failed and just check for that so why would there be any logical reason for using isset or empty to check a function return?
Use case: // Function definition: function getFriends() {
     // SQL SELECT or Fetching from XML or Fetching from LDAP or ...
     return $resultsAsArray;
} // Looping on results: foreach ( getFriends() as $friend ) {
     echo $friend["name"], "\n";
} // Case where the results are actually not iterated: $amIAssocial = empty( getFriends() ); 1. It it not wrong to return "nothing", like empty sets (empty arrays), it is a valid case. 2. Returning "false" in the case there is no "results" would be a bad idea: not only you would have to put a condition in the function definition, but you would also require all iterations (e.g. foreach) to be encapsulated in a condition statement to prevent looping on "false".
OK I'm just so used to if( $rs = $this->mDb->query( $query, $bindVars ) ) { while( $row = $rs->fetchRow() ) { } } So I can't see where 'empty( $rs->fetchRow() )' would fit into the work flow ... -- Lester Caine - G8HFL ----------------------------- Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/ Model Engineers Digital Workshop - http://medw.co.uk// Firebird - http://www.firebirdsql.org/index.php

« previous php.internals (#60439) next »