PHP 5.3 之前的数组中的关闭对象 [英] Closure objects within arrays before PHP 5.3

查看:23
本文介绍了PHP 5.3 之前的数组中的关闭对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 $exampleArray = array('功能' =>功能() {echo '这是一个例子';}

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

解决方案

如果你想在 PHP 中创建匿名 <5.3、可以使用create_function 函数.另外这里是有趣的信息关于回调(可能有用).

使用示例 create_function

# 这个(其他变量中的函数只是为了更干净的代码)$func = create_function('', "echo 'This is example from ananoymus function';");$exampleArray = 数组('功能' =>$func);

但是你可以用另一种方式做同样的事情,比如上面的代码:

# 创建一些函数函数 func(){# 做点什么echo '这是例子';}# 保存函数名$func = 'func';

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

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

第一种方式

$func();

替代方案

call_user_func($func);

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

function originalArrayStep(&$array, $function){#for循环,这里也可以使用foreachfor($i = 0; $i < count($array);$i++){# 检查 $function 是否可调用if( is_callable($function) ){# 调用函数$function(&$array[$i]);}别的{# 如果没有,在这里做点什么}}}

以及以上函数的使用:

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

';var_dump($array);

返回:

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

链接:

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.

解决方案

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:

这篇关于PHP 5.3 之前的数组中的关闭对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆