PHP Inner Functions And Closure
PHP Inner Functions And Closure
Written by Alex Armstrong   
Wednesday, 22 April 2015
Article Index
PHP Inner Functions And Closure
Object-oriented PHP
Closure


morephpRound

Closure

Unlike other languages where closures are automatically created in PHP you have to specify the variable that will be preserved and accessible to the anonymous function with the use construct. 

For example, if you try:

function MyFunction() {
 echo("My Function");
 $mylocalvar="testing";
 $MyInnerFunction = function() {
  echo($mylocalvar);
 };
 $MyInnerFunction();
}

MyFunction();

You will discover that $MyInnerFunction has no access to $mylocalvar. Even an anonymous function doesn't have access to the local variables of the function that contains it.

However you can give it access to any variables in the containing function using the use construct:

function MyFunction() {
 echo("My Function");
 $mylocalvar="testing";
 $MyInnerFunction = function() use( $mylocalvar ) {
 echo($mylocalvar);
};

$MyInnerFunction();
}

Now when you run the program you will see that the inner function does have access to $mylocalvar. It looks a lot like an extra parameter involved in the function call and indeed it is.

Notice that anonymous functions allow you do to things like pass a function as a parameter and return a function as a result. 

For example:

function MyFunction() {
 echo("My Function");
 $mylocalvar="testing";
 $MyInnerFunction = function() use( $mylocalvar ) {
  echo($mylocalvar);
 };

 return $MyInnerFunction;
}

$MyInnerFuncton=MyFunction();
$MyInnerFuncton();

Notice that now the inner function is returned as the result of MyFunction. You can call MyFunction and store the reference to the inner function in a variable. You can then call the inner function even though the outer function is complete and none of its local variables exist any more. The inner function still has access to the variables that you listed in the use construct.

There are two points to consider here.

The first is that closures aren't as useful in a server side language as they are in a client side language. The reason is simply that closures are useful in callbacks and event handling and server side PHP uses far fewer of both.

The second problem is that $this still isn't treated properly by the inner function.  That is you still have to use the $that trick and you still can't access private and protected members.

However anonymous functions do allow you to do a lot of things that would be difficult without them and they are worth knowing about. 

You may not find that inner functions or anonymous functions are something that you want to use but you may have them forced upon you. All you have to do is use a framework when you can write modules of some sort that are imported into other functions or classes and you can discover that any functions you do write are inner functions just as the result of how the framework uses them.

 

Related Articles

Ten minutes to PHP objects

Advanced loops

Object-oriented HTML Generation In PHP

What PHP does 

Ten minutes to PHP

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

Banner


Object-oriented HTML Generation In PHP

PHP is a programming language specifically designed to generate HTML pages but one of the topics that is rarely discussed is exactly how to do this. In this article the idea that HTML generation shoul [ ... ]



Getting Started With NetBeans PHP - Local Projects

NetBeans is primarily a Java IDE, but it also is a really good way to work with PHP and general HTML/JavaScript projects. The big problem is that the documentation isn't very good. Find out here how t [ ... ]


Other Articles

     

    blog comments powered by Disqus

     

    <ASIN:1847198384>

    <ASIN:0596803028>

    <ASIN:1430229195>

    <ASIN:1430228474>

    <ASIN:0596804377>

    <ASIN:0596159773>

     



    Last Updated ( Wednesday, 22 April 2015 )
     
     

       
    RSS feed of all content
    I Programmer - full contents
    Copyright © 2015 i-programmer.info. All Rights Reserved.
    Joomla! is Free Software released under the GNU/GPL License.