PHP 5.3 之前的数组中的闭包对象

发布于 2024-11-04 14:03:40 字数 270 浏览 2 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

豆芽 2024-11-11 14:03:40

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

使用示例 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"
}

链接:

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:

吃素的狼 2024-11-11 14:03:40

是的,可以使用 create_functionlamda 函数>。尽管您的问题提到但实际上并未使用,但不可能创建闭包

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

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

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

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

//similar to

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

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 };
相思故 2024-11-11 14:03:40
$exampleArray = array(
    'func' => create_function('', 'echo "this is an example"');
);

create_function

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

create_function

玩世 2024-11-11 14:03:40

只创建一个以“func”作为类方法的类怎么样?可以在 5.3 之前或之后工作。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文