Closure objects within arrays before PHP 5.3

荒凉一梦 提交于 2019-12-22 09:17:48

问题


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-anonymous by giving it a name, but this didn't seem to work.


回答1:


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()



回答2:


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 };



回答3:


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

create_function




回答4:


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



来源:https://stackoverflow.com/questions/5850778/closure-objects-within-arrays-before-php-5-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!