PHP 5.4 - '关闭 $this 支持'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5734011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:20:39  来源:igfitidea点击:

PHP 5.4 - 'closure $this support'

phpclosures

提问by jon_darkstar

I see that the new planned features for PHP 5.4 are: traits, array dereferencing, a JsonSerializable interface and something referred to as 'closure $this support'

我看到 PHP 5.4 的新计划特性是:特征、数组解引用、JsonSerializable 接口和称为“ closure $this support”的东西

http://en.wikipedia.org/wiki/Php#Release_history

http://en.wikipedia.org/wiki/Php#Release_history

While the others are either immediately clear (JsonSerialiable, array dereferencing) or i looked up the specifics (traits), I am not sure what 'closure $this support' is. I have been unsuccessful googling for it or finding anything about it on php.net

虽然其他人要么立即清楚(JsonSerialiable,数组取消引用),要么我查找了细节(特征),但我不确定什么是“关闭 $this support”。我一直没有成功搜索它或在 php.net 上找到任何关于它的信息

Does anyone know what this is supposed to be?

有谁知道这应该是什么?

If i had to guess, it would mean something like this:

如果我不得不猜测,它的意思是这样的:

$a = 10; $b = 'strrrring';
//'old' way, PHP 5.3.x
$myClosure = function($x) use($a,$b)
             {
                 if (strlen($x) <= $a) return $x;
                 else return $b;
             };

//'new' way with closure $this for PHP 5.4
$myNewClosure = function($x) use($a as $lengthCap,$b as $alternative)
                 {
                     if(strlen($x) <=  $this->lengthCap)) return $x;
                     else 
                     {
                         $this->lengthCap++;  //lengthcap is incremented for next time around
                         return $this->alternative;
                     }
                 };

The significance (even if this example is trivial) being that in the past once the closure is constructed the bound 'use' variables are fixed. With 'closure $this support' they are more like members you can mess with.

意义(即使这个例子是微不足道的)是在过去一旦构建了闭包,绑定的“使用”变量就固定了。通过“关闭 $this support”,他们更像是您可以惹恼的成员。

Does this sound correct and/or close and/or reasonable? Does anyone know what this 'closure $this support' means?

这听起来正确和/或接近和/或合理吗?有谁知道这个“关闭 $this support”是什么意思?

回答by Gordon

This was already planned for PHP 5.3, but

这已经为 PHP 5.3 计划了,但是

For PHP 5.3 $this support for Closures was removed because no consensus could be reached how to implement it in a sane fashion. This RFC describes the possible roads that can be taken to implement it in the next PHP version.

在 PHP 5.3 $ 中删除了对闭包的支持,因为无法就如何以理智的方式实现它达成共识。该 RFC 描述了在下一个 PHP 版本中实现它的可能途径。

It indeed means you can refer to the object instance (live demo)

这确实意味着您可以参考对象实例(现场演示

<?php
class A {
  private $value = 1;
  public function getClosure() 
  {
    return function() { return $this->value; };
  }
}

$a = new A;
$fn = $a->getClosure();
echo $fn(); // 1

For a discussion, see the PHP Wiki

有关讨论,请参阅 PHP Wiki

and for historic interest:

为了历史利益:

回答by igorw

One thing that Gordon missed is re-binding of $this. While what he described is the default behaviour, it is possible to re-bind it.

戈登错过的一件事是重新绑定$this. 虽然他描述的是默认行为,但可以重新绑定它。

Example

例子

class A {
    public $foo = 'foo';
    private $bar = 'bar';

    public function getClosure() {
        return function ($prop) {
            return $this->$prop;
        };
    }
}

class B {
    public $foo = 'baz';
    private $bar = 'bazinga';
}

$a = new A();
$f = $a->getClosure();
var_dump($f('foo')); // prints foo
var_dump($f('bar')); // works! prints bar

$b = new B();
$f2 = $f->bindTo($b);
var_dump($f2('foo')); // prints baz
var_dump($f2('bar')); // error

$f3 = $f->bindTo($b, $b);
var_dump($f3('bar')); // works! prints bazinga

The closures bindToinstance method (alternatively use the static Closure::bind) will return a new closure with $thisre-bound to the value given. The scope is set by passing the second argument, this will determine visibility of private and protected members, when accessed from within the closure.

闭包bindTo实例方法(或者使用 static Closure::bind)将返回一个$this重新绑定到给定值的新闭包。作用域是通过传递第二个参数来设置的,当从闭包内部访问时,这将确定私有成员和受保护成员的可见性。

回答by Xeoncross

Building on @Gordon's answer it is posible to mimic some hacky aspects of closure-$this in PHP 5.3.

以@Gordon 的回答为基础,可以在 PHP 5.3 中模仿闭包的一些 hacky 方面-$this。

<?php
class A
{
    public $value = 12;
    public function getClosure()
    {
        $self = $this;
        return function() use($self)
        {
            return $self->value;
        };
    }
}

$a = new A;
$fn = $a->getClosure();
echo $fn(); // 12

回答by Harry Mustoe-Playfair

Just building on the other answers here, I think this example will demonstrate what is possible PHP 5.4+:

仅以此处的其他答案为基础,我认为此示例将演示 PHP 5.4+ 的可能性:

<?php

class Mailer {
    public    $publicVar    = 'Goodbye ';
    protected $protectedVar = 'Josie ';
    private   $privateVar   = 'I love CORNFLAKES';

    public function mail($t, $closure) {
        var_dump($t, $closure());
    }
}

class SendsMail {
    public    $publicVar    = 'Hello ';
    protected $protectedVar = 'Martin ';
    private   $privateVar   = 'I love EGGS';

    public function aMailingMethod() {
        $mailer = new Mailer();
        $mailer->mail(
            $this->publicVar . $this->protectedVar . $this->privateVar,
            function() {
                 return $this->publicVar . $this->protectedVar . $this->privateVar;
            }
        );
    }
}

$sendsMail = new SendsMail();
$sendsMail->aMailingMethod();

// prints:
// string(24) "Hello Martin I love EGGS"
// string(24) "Hello Martin I love EGGS"

see: https://eval.in/private/3183e0949dd2db

见:https: //eval.in/private/3183e0949dd2db