PHP中的闭包......它们究竟是什么以及何时需要使用它们?

问题描述 投票:76回答:7

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

php oop closures
7个回答
74
投票

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

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

这里给出了一个很好的解释What are php lambdas and closures


15
投票

如果您将来需要一个执行您现在已决定的任务的功能。

例如,如果您读取配置文件,其中一个参数告诉您算法的hash_methodmultiply而不是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)
}

15
投票

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

在PHP的上下文中,它们有点奇怪,因为PHP已经在基于类,面向对象的范例以及较旧的程序范例上沉重。通常,具有闭包的语言具有完整的词法范围。为了保持向后兼容性,PHP不会得到这个,所以这意味着闭包在这里会比在其他语言中有所不同。我想我们还没有看到它们将如何被使用。


10
投票

我喜欢troelskn的帖子提供的上下文。当我想在Dan中使用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

当然,如果你想让它随处可用,你可以让一切都变得静止......


2
投票

闭包基本上是一个函数,您可以在一个上下文中编写定义,但在另一个上下文中运行。 Javascript帮助我理解了这些,因为它们在JavaScript中被广泛使用。

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

这就是关闭的内容,它应该足以理解上面所写的内容。

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

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


1
投票

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

 <?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;

0
投票

以下是php中闭包的示例

// Author: [email protected]
// 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
© www.soinside.com 2019 - 2024. All rights reserved.