25

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
  • 505
  • 5
  • 19
  • 1
    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
  • @dandavis why *doesn't* PHP put the same priority on global-avoidance? (new to PHP from JS, so I really don't know) – Scribblemacher Dec 12 '22 at 17:02
  • @Scribblemacher:speculation, but it's typically (non fpm) short-lived (less chances of hard-to-debug conflicts), it has a _ton_ of its own globals compared to js (tradition, less reliance on hastily-written helper methods), and it tends to use a lot less 3rd party code (code that assumes a squeaky clean environ). – dandavis Dec 12 '22 at 21:34

2 Answers2

51

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,528
  • 1
  • 24
  • 37
  • 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
  • 3
    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
0

You can also immediately invoke annonymous class in php.

<?php new class (0,1) {
  public function __construct (int $a, int $b) {

    (($a < 1) ? 'var_dump' : 'var_export')(
      gettype (($b > 0) ? __FUNCTION__ 
        : (($a > 0) ? microtime (true) 
        : __LINE__
    )))&exit;
    
  }
};
Spooky
  • 1,235
  • 15
  • 17