This is another article in my attempts to find new ways of looking at PHP and making it less of a chore to type in all that code. As much as I love PHP I hate wasting keystrokes. More typing is more errors is more grief.
Being an off and on user of Lisp, although not as much as I used to, one of the things that I always liked in Lisp was the ability to be able to return multiple values from a function at once using (values) and then marry that with (multiple-value-bind) to create convenient named bindings for whatever you were about to do.
I recently found myself wanting to return a couple of values from a helper function and I just didn't want to go to the trouble of having to type all those character required to create an array with keys for the two values and then I remembered MVB and a little light went on in my head!
If somebody else has already done this then I apologise up front but it was new to me and I haven't seen it anywhere else so this could be a first!
Returning multiple values... the usual way
Traditionally in PHP you could always return an Array of something, be it a keyed array or indexed. The bind (no pun intended) of course is that on the receiving end of that return data you then have to either know what's what (indexed) or type out lots of repetitive code (keys) to access those values. Sure, if you are a sloppy lazy I-don't-care-so-long-as-it-works code monkey then you probably don't want to get better at what you do but for the rest who like to find new ways to do things, read on!
Here's a little function that checks the $_GET variable and returns two flags depending upon a make believe direction flag in the request:
/* * Set in and out direction flags from request data * * @param Array $arrOptions contains the flag set to check * * @return Array with 'inbound' and 'outbound' set/reset. */ protected function getInOut($arrOptions) { $arrOut = array( 'inbound' => true, 'outbound' => true); $strDir = isset($arrOptions['direction']) ? $_GET['direction' : 'all'; switch($strDir) { case 'inbound': $arrOut['inbound'] = true; break; case 'outbound': $arrOut['outbound'] = true; break; } return $arrOut; } // and called like this .. $arrFlags = getInOut($_GET);
I try never to bury the superglobals into core code as it makes it less flexible for testing and re-use.
We can see that we'd end up with an array and that to be able to use those flags we'd then have to litter code with:
if ($arrFlags['inbound']) { ... } if ($arrFlags['outbound']) { ... }
OK, not the end of the world I grant you, but a little tiresome. You could of course immediately extract the returned data into variables and then re-code the above like this:
// modified version of the function... protected function getInOut($arrOptions) { $arrOut = array(true, true); // note: no keys now! $strDir = isset($arrOptions['direction']) ? $_GET['direction' : 'all'; switch($strDir) { case 'inbound': $arrOut[0] = true; break; case 'outbound': $arrOut[1] = true; break; } return $arrOut; // indexed array now } $arrFlags = getInOut($_GET); $blInbound = $arrFlags['inbound']; $blOutbound = $arrFlags['outbound']; if ($blInbound) { ... } if ($blOutbound) { ... }
And the Eureka moment is...
Realising that the previous step of calling the function and then extracting the two values into locally named variables is something that you can already do with a stock PHP function: list() !!!
Yes, by using list() you can bind the return variables from the returned array in one clean sweep! This means the code is slightly smaller and you didn't have to waste time creating array keys that you really could do without, like this:
$arrFlags = getInOut($_GET); $blInbound = $arrFlags['inbound']; $blOutbound = $arrFlags['outbound']; becomes.... list($blInbound, $blOutbound) = getInOut($_GET);
To me and my warped brain that actually reads better; it's short, concise and to the point, sacrifices nothing in the way of readability and does what it says on the tin.
Yes, you have to know what order they are returned in whereas a keyed array alleviates you from knowing that, but if the code you are writing doesn't warrant that kind of freedom then this is a cool little trick, that I for one will trying to slip in more often now I unearthed it!
There is always extract() as well of course, which would achieve the same thing but I find that approach unclean most of the time. It always feels a little edgy using it, and you'd have to return the original keyed array instead. Plus you have the added responsibility of making sure there are no variable clashes etc.
So there you go, another interesting way of dealing with PHP and an attempt to reduce typing but not at the expense of readability!
Comments
Post a Comment