87

所以我正在以一种不错的、最新的、面向对象的方式进行编程。我经常使用 PHP 实现的 OOP 的各个方面,但我想知道什么时候需要使用闭包。有没有专家可以阐明何时实施闭包有用?

4

8 回答 8

87

PHP 将在 5.3 中原生支持闭包。当您想要一个仅用于一些小的、特定目的的本地函数时,闭包是很好的选择。闭包的RFC给出了一个很好的例子:

function replace_spaces ($text) {
    $replacement = function ($matches) {
        return str_replace ($matches[1], ' ', ' ').' ';
    };
    return preg_replace_callback ('/( +) /', $replacement, $text);
}

这使您可以replacement在内部本地定义函数replace_spaces(),这样就不会:
1)弄乱全局名称空间
2)让人们在三年后想知道为什么全局定义的函数只在另一个函数内部使用

它使事情井井有条。请注意函数本身没有名称,它只是被定义并分配为对$replacement.

但请记住,您必须等待 PHP 5.3 :)

您还可以使用关键字将其范围之外的变量访问到闭包中use。考虑这个例子。

// Set a multiplier  
 $multiplier = 3;

// Create a list of numbers  
 $numbers = array(1,2,3,4);

// Use array_walk to iterate  
 // through the list and multiply  
 array_walk($numbers, function($number) use($multiplier){  
 echo $number * $multiplier;  
 }); 

这里给出了一个很好的解释什么是php lambdas和闭包

于 2008-09-30T14:41:40.093 回答
17

当您将来需要一个功能来执行您现在决定的任务时。

例如,如果您读取一个配置文件并且其中一个参数告诉hash_method您算法的 是multiply而不是square,您可以创建一个闭包,该闭包将在您需要散列某些内容的任何地方使用。

可以在(例如)中创建闭包config_parser();它创建一个do_hash_method()使用本地变量调用的函数config_parser()(来自配置文件)。每当do_hash_method()被调用时,它都可以访问本地范围内的变量,config_parser()即使它没有在该范围内被调用。

希望是一个很好的假设示例:

function config_parser()
{
    // Do some code here
    // $hash_method is in config_parser() local scope
    $hash_method = 'multiply';

    if ($hashing_enabled)
    {
        function do_hash_method($var)
        {
            // $hash_method is from the parent's local scope
            if ($hash_method == 'multiply')
                return $var * $var;
            else
                return $var ^ $var;
        }
    }
}


function hashme($val)
{
    // do_hash_method still knows about $hash_method
    // even though it's not in the local scope anymore
    $val = do_hash_method($val)
}
于 2008-09-28T21:23:06.183 回答
16

除了技术细节之外,闭包是被称为面向函数编程的编程风格的基本先决条件。闭包的用途与在面向对象编程中使用对象大致相同。它将数据(变量)与一些代码(一个函数)绑定在一起,然后您可以将其传递到其他地方。因此,它们会影响您编写程序的方式,或者——如果你不改变编写程序的方式——它们根本没有任何影响。

在 PHP 的上下文中,它们有点奇怪,因为 PHP 已经很重基于类、面向对象的范式以及较旧的过程范式。通常,具有闭包的语言具有完整的词法范围。为了保持向后兼容性,PHP 不会实现这一点,这意味着这里的闭包将与其他语言有所不同。我认为我们还没有确切地看到它们将如何被使用。

于 2008-09-28T22:22:09.817 回答
10

我喜欢 troelskn 的帖子提供的上下文。当我想在 PHP 中做类似 Dan Udey 的示例时,我使用 OO 策略模式。在我看来,这比引入一个行为在运行时确定的新全局函数要好得多。

http://en.wikipedia.org/wiki/Strategy_pattern

您还可以使用 PHP 中保存方法名称的变量来调用函数和方法,这很棒。所以对丹的例子的另一种看法是这样的:

class ConfigurableEncoder{
        private $algorithm = 'multiply';  //default is multiply

        public function encode($x){
                return call_user_func(array($this,$this->algorithm),$x);
        }

        public function multiply($x){
                return $x * 5;
        }

        public function add($x){
                return $x + 5;
        }

        public function setAlgorithm($algName){
                switch(strtolower($algName)){
                        case 'add':
                                $this->algorithm = 'add';
                                break;
                        case 'multiply':        //fall through
                        default:                //default is multiply
                                $this->algorithm = 'multiply';
                                break;
                }
        }
}

$raw = 5;
$encoder = new ConfigurableEncoder();                           // set to multiply
echo "raw: $raw\n";                                             // 5
echo "multiply: " . $encoder->encode($raw) . "\n";              // 25
$encoder->setAlgorithm('add');
echo "add: " . $encoder->encode($raw) . "\n";                   // 10

当然,如果您希望它随处可用,您可以将所有内容都设为静态...

于 2008-09-29T00:44:24.047 回答
3

闭包基本上是一个函数,您在一个上下文中为其编写定义但在另一个上下文中运行。Javascript 在理解这些方面帮助了我很多,因为它们在 JavaScript 中无处不在。

在 PHP 中,由于函数内部“全局”(或“外部”)变量的范围和可访问性不同,它们不如 JavaScript 有效。然而,从 PHP 5.4 开始,闭包可以在对象内部运行时访问 $this 对象,这使得它们更加有效。

这就是闭包的意义所在,理解上面写的内容应该就足够了。

这意味着应该可以在某处编写函数定义,并在函数定义中使用 $this 变量,然后将函数定义分配给变量(其他人已经给出了语法示例),然后将此变量传递给对象并在对象上下文中调用它,然后该函数可以通过 $this 访问和操作对象,就好像它只是它的另一个方法一样,而实际上它不是在该对象的类定义中定义的,而是在其他地方定义的。

如果不是很清楚,请不要担心,一旦开始使用它们就会变得清晰。

于 2014-08-25T18:46:55.770 回答
1

基本上,闭包是可以访问外部变量的内部函数,并用作匿名函数(没有任何名称的函数)的回调函数。

 <?php
      $param='ironman';
      function sayhello(){
          $param='captain';
          $func=function () use ($param){
                $param='spiderman';
          };
       $func();
       echo  $param;
       }
      sayhello();
?>

//output captain

//and if we pass variable as a reference as(&$param) then output would be spider man;
于 2019-03-04T10:49:21.623 回答
1

闭包:

MDN 对 IMO 有最好的解释:

闭包是捆绑在一起(封闭)的函数与对其周围状态(词法环境)的引用的组合。换句话说,闭包使您可以从内部函数访问外部函数的范围。

即闭包是一个可以访问父作用域中的变量的函数。闭包允许我们方便地动态创建函数,因为在某些情况下,函数只需要在一个地方(回调、可调用参数)。

例子:

$arr = [1,2,3,3];
$outersScopeNr = 2;

// The second arg in array_filter is a closure
// It would be inconvenient to have this function in global namespace
// The use keyword lets us access a variable in an outer scope
$newArr = array_filter($arr, function ($el) use ($outersScopeNr) {
    return $el === 3 || $el === $outersScopeNr;
});

var_dump($newArr);
// array (size=3)
//  1 => int 2
//  2 => int 3
//  3 => int 3
于 2020-12-11T13:30:52.320 回答
0

以下是 php 中的闭包示例

// Author: HishamDalal@gamil.com
// Publish on: 2017-08-28

class users
{
    private $users = null;
    private $i = 5;

    function __construct(){
        // Get users from database
        $this->users = array('a', 'b', 'c', 'd', 'e', 'f');
    }

    function displayUsers($callback){
        for($n=0; $n<=$this->i; $n++){
            echo  $callback($this->users[$n], $n);
        }
    }

    function showUsers($callback){
        return $callback($this->users);

    }

    function getUserByID($id, $callback){
        $user = isset($this->users[$id]) ? $this->users[$id] : null;
        return $callback($user);
    }

}

$u = new users();

$u->displayUsers(function($username, $userID){
    echo "$userID -> $username<br>";
});

$u->showUsers(function($users){
    foreach($users as $user){
        echo strtoupper($user).' ';
    }

});

$x = $u->getUserByID(2, function($user){

    return "<h1>$user</h1>";
});

echo ($x);

输出:

0 -> a
1 -> b
2 -> c
3 -> d
4 -> e
5 -> f

A B C D E F 

c
于 2017-08-28T14:35:43.370 回答