Skip to main content

PHP and Lisp: multiple-value-bind (MVB)

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

Popular posts from this blog

The Coolest Shortest PHP Function I Will Ever Write

Having now released my own programming language, FELT , and learned a lot about this and that in the process I have of late, in the evenings, been struggling to reconcile my love of LISP and how simple FELT makes some PHP coding task leaner and meaner with the fact that I still have to use PHP for my day job. In my language, FELT , I have used the square brackets to define a "normal" array and curly braces to define a "key-value" array, mainly because this is identical to JSON format and anybody familiar with Javascript coding just won't have any issues getting to grips with that now will they! Let's take some simple examples of FELT code: (defvar simple-array [1 2 3 4]) (defvar simple-map {:name "Eric" :age 42 :occupation "Viking Hacker"}) When FELT has done its thing, we get the following PHP code, $simple_array = array(1, 2, 3, 4); $simple_map = array('name' => "Eric", 'age' => 42, ...

Handling multipart/form-data with NanoHTTPD

I am in the process of reviving an old project from 2014 that I never finished because of other work commitments. In that time, bitrot has set in, the Android API has moved on and all in all, the home-brewed HTTP server I wrote using SocketServer and the org.apache libraries had to go! I looked around, found a couple of contenders and after much time decided to go with NanoHTTPD because it is lean, small and fits in exactly two files. The main server is in one file `NanoHTTPD.java`and there is another file called `ServerRunner.java` which manages instances of running servers. The others The other project I looked at is this one:  https://github.com/koush/AndroidAsync which led me a merry dance and I just couldn't figure out how get the POST data I had uploaded. I spent a few days really digging at it with Wire Shark too to make sure the data was going up. It was. Whatever... I had used it via a gradle dependency entry but I dropped it and went back to NanoHTTPD. For m...

Using a RAM disk with Opera on OS X

Having recently configured AndroidStudio to use a RAM disk for Gradle, I thought I would look around and see if I can use the remaining space for Opera. This is essentially a reproduction of this fine page: http://www.ghacks.net/2010/10/20/how-to-change-the-opera-cache-directory/ That page does not deal with Macs though and after a little bit of experimentation I came up with this spell: open /Applications/Opera.app/ --args --disk-cache-dir=/Volumes/RamDisk/opera For the record, here is my Opera version: Make sure that the specified folder exists before starting Opera, if might automatically create the folder for you but I didn't bother to find out, I hate disappointment. And for the record, the way I create a RAM disk on my iMac, which is done automatically when I log in, is like this: diskutil erasevolume HFS+ "RamDisk" `hdiutil attach -nomount ram://4194304` The above line was courtesy of this YouTube video: Thanks to Bartech TV then! So, with Turbo m...