开发者

Closure objects within arrays before PHP 5.3

I know it's possible to do the following with PHP 5.3 (anonymous functions), but is there a similar alternative in older PHP version (pre-5.3)?

  $exampleArray = array(  
    'func' => function() {  
      echo 'this is an example';  
      }

Is it possible to do this with __call or typecasting the function as an (object) first? Also, I tried making the function un-an开发者_Go百科onymous by giving it a name, but this didn't seem to work.


If you want to create anonymous in PHP < 5.3, you can use create_function function. Also Here is interesting information about callbacks (may be usefull).

Example using create_function

# This (function in other variable is only for cleaner code)
$func = create_function('', "echo 'This is example from anoymus function';");

$exampleArray = array(
  'func' => $func
  );

But you can do the same thing liek code above with alternative way:

# Create some function
function func()
{
   # Do something
   echo 'This is example';
}
# Save function name
$func = 'func';

Code above creates function which does something, then we store function name in variable (can be passed as parameter, etc.).

Calling function when we know only it's name:

First way

$func();

Alternative

call_user_func($func);

So example that connects everything above:

function primitiveArrayStep(&$array, $function)
{
    # For loop, foreach can also be used here
    for($i = 0; $i < count($array);$i++)
    {
         # Check if $function is callable             
          if( is_callable($function) )
          {
               # Call function
           $function(&$array[$i]);
          }
          else
          {
               # If not, do something here
          }

    }    
}

And use of above function:

$array = array('a', 'b', 'c');

$myFunction = create_function('&$e', '$e = $e . " and i was here";');

primitiveArrayStep($array, $myFunction);

echo '<pre>';
var_dump($array);

Returns:

array(3) {
  [0]=>
  string(16) "a and i was here"
  [1]=>
  string(16) "b and i was here"
  [2]=>
  string(16) "c and i was here"
}

Links:

  • Callbacks
  • create_function()
  • call_user_func()
  • is_callable()


Yes it is possible to create lamda functions with PHP pre 5.3 using create_function. It isn't possible to create closures though which your question mentions but doesn't actually use.

A closure is a lamda function that has access (closes over) a variable from it's enclosing scope:

$t = new Thingy;
$func = function( $y ) use( $t ) {
    //$t is available here when this function is called;
}

A lamda function is an anonymous function useful for storing in a variable or passing as an argument etc. You can use create_function() pre 5.3 like this:

$func = create_function( '$y', 'echo $y;' );

//similar to

$func = function( $y ){ echo $y };


$exampleArray = array(
    'func' => create_function('', 'echo "this is an example"');
);

create_function


What about just creating a class with 'func' as a class method? Would work pre or post 5.3.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜