PHP 7: Immediately Invoked Function Expressions


PHP7 will continue to borrow some of the beloved JavaScript features and will support Immediately Invoked Function Expressions (IIFEs):


echo (function() {
    return 42;
Output for [email protected] - 20141101:

Since we can already return functions from inside other functions, Currying Partial Application is also possible in combination with the IIFE implementation:


$foo = (function() {
    return function($a) {
        return $a + 42;

echo $foo(10);
Output for [email protected] - 20141101:

Note that when accessing an outer variable from within a function, one – unlike in JavaScript which has closures – needs to use the use keyword to make that variable accessible:


$foo = (function($a) {
    return function($b) use ($a) {
        return $a + $b;
echo $foo(10);
Output for [email protected] - 20141101:

/me is excited!

PHP7 IIFE Demo →
PHP7 Currying Demo →
PHP7 Currying Redux Demo →

Published by Bramus!

Bramus is a Freelance Web Developer from Belgium. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Join the Conversation


  1. I was just trying to do this in PHP today and was a little sad finding out that you can’t. But I’m very glad to see these changes coming! (soon?)

      1. Bro good job. I can execute logical expression with your IIFE –

        $a = [‘no’,’no’, ‘yes’, ‘np’, 787];
        if (in_array(‘yes’, $a)) echo “hello”;


        in_array(‘yes’, $a) && call_user_func(function() {
        echo “hello”;

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.