【问题标题】:Closure objects within arrays before PHP 5.3PHP 5.3 之前的数组中的闭包对象
【发布时间】:2011-08-16 14:02:56
【问题描述】:

我知道可以使用 PHP 5.3(匿名函数)执行以下操作,但在旧的 PHP 版本(5.3 之前)中是否有类似的替代方法?

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

是否可以通过 __call 或首先将函数类型转换为(对象)来做到这一点?另外,我尝试通过给它一个名称来使该函数不匿名,但这似乎不起作用。

【问题讨论】:

    标签: php anonymous-function php-5.2


    【解决方案1】:
    $exampleArray = array(
        'func' => create_function('', 'echo "this is an example"');
    );
    

    create_function

    【讨论】:

    • 鼓舞人心的答案。我学到了很多东西。感觉这个答案值得更多关注。这样的 PHP 力量。
    【解决方案2】:

    如果你想在 PHP create_function 函数。 Here 也是关于回调的有趣信息(可能有用)。

    使用示例 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
      );
    

    但是您可以通过其他方式对上面的代码执行相同的操作:

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

    上面的代码创建了函数,然后我们将函数名存储在变量中(可以作为参数传递等)。

    当我们只知道它的名字时调用函数:

    第一种方式

    $func();
    

    替代方案

    call_user_func($func);
    

    连接以上所有内容的示例:

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

    以及上述功能的使用:

    $array = array('a', 'b', 'c');
    
    $myFunction = create_function('&$e', '$e = $e . " and i was here";');
    
    primitiveArrayStep($array, $myFunction);
    
    echo '<pre>';
    var_dump($array);
    

    返回:

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

    链接:

    【讨论】:

      【解决方案3】:

      是的,可以使用create_function 使用 PHP 5.3 之前的版本创建 lamda 函数。不可能创建 闭包,尽管您的问题提到但实际上并未使用。

      闭包是一个 lamda 函数,它可以从其封闭范围访问(关闭)一个变量:

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

      lamda 函数是一个匿名函数,可用于存储在变量中或作为参数传递等。您可以像这样使用create_function() pre 5.3:

      $func = create_function( '$y', 'echo $y;' );
      
      //similar to
      
      $func = function( $y ){ echo $y };
      

      【讨论】:

        【解决方案4】:

        如果只创建一个使用 'func' 作为类方法的类呢?可以在 5.3 之前或之后工作。

        【讨论】:

          猜你喜欢
          • 2012-12-23
          • 2014-03-20
          • 1970-01-01
          • 2012-01-24
          • 1970-01-01
          • 2017-02-12
          • 1970-01-01
          • 1970-01-01
          • 2011-05-03
          相关资源
          最近更新 更多