19

I wonder if PHP has any equivalence of IIFE (Immediately Invoked Function Expression) like that of Javascript. Can PHP Closure be written anyhow so it can work similarly to Javascript (invoking, dependency, injection, directive) ?

(function(){
    myModule = angular.module('myAngularApplication', []);
}());

This expression above is known as Immediately invoked function expression(IIFE). Since the function definition will immediately invoke itself whenever the .js file is loaded. The main reason the IIFE is effective is that we can have all the code immediately executing without needing to have global variables and functions. Now when we do this, our controller creation will fail as we were using the global variable to create controller with the module. To circumvent this problem lets use the getter function angular.module to associate the controller with the module. And while we are at it, why not put the controller in an IIFE too.

(function () {

    var booksController = function ($scope) {
         $scope.message = "Hello from booksController";
    }

    angular.module('myAngularApplication').controller('booksController', booksController); 
}());

Source: http://www.codeproject.com/Articles/995498/Angular-Tutorial-Part-Understanding-Modules-and Thank you.

Pristine Kallio
  • 445
  • 4
  • 19
  • php code doesn't place the same priority on global-avoidance that JS does, for a host of reasons. – dandavis Jan 27 '16 at 17:45
  • The anonymous IIFE pattern in JS is to provide some semblance of private variables (since ES5 variables were function-scope only). The pattern expands to `function foo() {...}; foo();` which can be done in any language. – ssube Jan 27 '16 at 17:45
  • 3
    except that your IIFE does create a global variable. O.o – Kevin B Jan 27 '16 at 17:45
  • Possible duplicate of [Creating and invoking an anonymous function in a single statement](http://stackoverflow.com/questions/3605595/creating-and-invoking-an-anonymous-function-in-a-single-statement) – Mike Cluck Jan 27 '16 at 17:46
  • You can always namespace your code so you don't pollute the global namespace. – MinusFour Jan 27 '16 at 17:50
  • @dandavis yes, every time writing PHP code, I have to make sure no duplication in naming convention by using prefixes $t_my_var for temporary, $g_my_var for global, scoping with private and enclosing with class MySuperClosure{ } to avoid duplication. – Pristine Kallio Jan 27 '16 at 17:52
  • 1
    Have you thought about reconsidering your design instead of trying to force JS' broken (yeah yeah I know) scoping solutions into your php projects? – PeeHaa Jan 27 '16 at 18:26

1 Answers1

44

In PHP 7, yes, you can:

(function() { echo "yes, this works in PHP 7.\n"; })();

This does not work in PHP 5.x. Instead, the closest you can come is:

call_user_func(function() { echo "this works too\n"; });
jbafford
  • 5,308
  • 1
  • 26
  • 34
  • Oh, that would be nice but my IDE provider on the cloud still services PHP at 5.5.9. – Pristine Kallio Jan 27 '16 at 17:55
  • 2
    It prevents accidental use of globals, so yes, it is often a good idea--though PHP's formal namespace mechanism may minimize the need. – Brett Zamir Mar 30 '19 at 01:20
  • 1
    Another option is to define it into a variable - this is useful if you need to call it multiple times, e.g. $func = function($param) { echo $param; }; $func('Hello'); – GuruBob Aug 10 '20 at 21:50