JavaScript中的“call on declare”匿名函数相当于?

时间:2021-05-11 22:28:47

I have some code that i want to be executed in a PHP script, but i don't want the variable created in this script to be visible otherwhere.

我有一些代码,我想在PHP脚本中执行,但我不希望在此脚本中创建的变量在其他地方可见。

What i would like to do is like we can do using JavaScript :

我想做的就是我们可以使用JavaScript:

(function() {
    // Do some stuff
})();

But i'd like to do this with PHP.

但我想用PHP做这件事。

I've imagined the following code :

我想象了以下代码:

$main = function()
{
    global $main;
    unset($main);

    // Do some stuff
}

$main();

BUt it's not supported before PHP 5.3, and i need it to be.

在PHP 5.3之前不支持它,我需要它。

Is there another way to do this with PHP 5.2 ?

还有另一种方法可以用PHP 5.2做到这一点吗?

Thanks in advance :)

提前致谢 :)

2 个解决方案

#1


0  

call_user_func(
    // create_function('$param1, $param2', '
    function($param1, $param2) { 
        // do stuff
    }
    // ')
, "param1", "param2");

Use the lines commented out for php < 5.3

使用注释掉的行

But I think many people will agree this is a weird thing to do.

但我想很多人会同意这是一件很奇怪的事情。

#2


0  

You can use create_function() but this is ugly i think.

你可以使用create_function(),但我觉得这很难看。

(I have a bigger problem with your code sample and maybe with your real code: you are using global state which is really bad.)

(我的代码示例有一个更大的问题,也许你的真实代码:你正在使用非常糟糕的全局状态。)

#1


0  

call_user_func(
    // create_function('$param1, $param2', '
    function($param1, $param2) { 
        // do stuff
    }
    // ')
, "param1", "param2");

Use the lines commented out for php < 5.3

使用注释掉的行

But I think many people will agree this is a weird thing to do.

但我想很多人会同意这是一件很奇怪的事情。

#2


0  

You can use create_function() but this is ugly i think.

你可以使用create_function(),但我觉得这很难看。

(I have a bigger problem with your code sample and maybe with your real code: you are using global state which is really bad.)

(我的代码示例有一个更大的问题,也许你的真实代码:你正在使用非常糟糕的全局状态。)