Javascript闭包和PHP闭包,有什么区别? [英] Javascript closures vs PHP closures, what's the difference?

查看:142
本文介绍了Javascript闭包和PHP闭包,有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JS中的closures和PHP中的closures有什么区别?他们几乎工作是一样的方式吗?在PHP中编写闭包时有什么需要注意的吗?

解决方案

一个区别是如何处理存储执行匿名函数的上下文:

  // JavaScript:
var a = 1;
var f = function(){
console.log(a);
};
a = 2;
f();
//将回显2;

// PHP
$ a = 1;
$ f = function(){
echo $ a;
};
$ a = 2;
$ f();
//将导致PHP Notice:Undefined variable:a in Untitled.php on line 5

要修复此通知,您必须使用使用语法:

  $ a = 1; 
$ f = function()using($ a){
echo $ a;
};
$ a = 2;
$ f();
//但这将回显1而不是2(如JavaScript)

匿名函数以某种方式表现得像JavaScript对等体,你必须使用引用:

  $ a = 1; 
$ f = function()use(& $ a){
echo $ a;
};
$ a = 2;
$ f()
//将回显2

我认为这是JavaScript和PHP之间最明显的区别

是每个JavaScript闭包都有一个 this 上下文意味着你可以在闭包本身中使用 this (虽然通常很复杂的是找出这个 )PHP的当前稳定版本(PHP 5.3)目前还不支持 $ this ,但PHP的即将推出的版本(PHP 5.4)将支持 $ this 使用 $ closure-> bind($ this)绑定和重新绑定(请参阅对象扩展RFC 了解详情。)



第三个区别是两种语言如何处理分配给对象属性的闭包:

  // JavaScript 
var a = {
b:function(){}
};
a.b(); // works


// PHP
$ a = new stdClass();
$ a-> b = function(){};
$ a-> b(); //不工作PHP致命错误:调用未定义的方法stdClass :: b()在Untitled.php第4行

$ f = $ a-> b;
$ f(); //工作虽然

如果闭包分配给类定义中的属性,同样如此:

  A类{
public $ b;

public function __construct(){
$ this-> b = function(){};
}

public function c(){
$ this-> b();
}
}
$ a = new A();
//没有
$ a-> b();
//或
$ a-> c();
// do work

第四个区别:JavaScript Closures完全成熟的对象,在PHP中他们是受限制的对象。例如,PHP Closures不能有自己的属性:

  $ fn = function(){}; 
$ fn-> foo = 1;
// - > Catchable致命错误:在JavaScript中,Closure对象不能具有属性

b
$ b

  var fn = function(){}; 
fn.foo = 1;
fn.foo; // 1

第五个区别:返回的闭包可以立即调用Javascript:

  var fn = function(){return function(){alert('Hi');}} 
fn()();

不在PHP中:

  $ fn = function(){return function(){echo('Hi');};}; 
$ fn()(); //语法错误


What are the differences between closures in JS and closures in PHP? Do they pretty much work the same way? Are there any caveats to be aware of when writing closures in PHP?

解决方案

One difference is how both cope with storing the context in which an anonymous function is executed:

// JavaScript:
var a = 1;
var f = function() {
   console.log(a);
};
a = 2;
f();
// will echo 2;

// PHP
$a = 1;
$f = function() {
    echo $a;
};
$a = 2;
$f();
// will result in a "PHP Notice:  Undefined variable: a in Untitled.php on line 5"

To fix this notice you'll have to use the use syntax:

$a = 1;
$f = function() use ($a) {
    echo $a;
};
$a = 2;
$f();
// but this will echo 1 instead of 2 (like JavaScript)

To have the anonymous function behave somehow like the JavaScript counterpart you'll have to use references:

$a = 1;
$f = function() use (&$a) {
    echo $a;
};
$a = 2;
$f();
// will echo 2

I think this is the most striking difference between JavaScript and PHP closures.

Second difference is that every JavaScript closure has a this context available which means, that you can use this inside the closure itself (although it's often quite complicated to figure out what this actually refers to) - PHP's current stable version (PHP 5.3) does not yet support $this inside a closure, but PHP's upcoming version (PHP 5.4) will support $this binding and rebinding using $closure->bind($this) (See the Object Extension RFC for more info.)

Third difference is how both languages treat closures assigned to object properties:

// JavaScript
var a = {
    b: function() {}
};
a.b(); // works


// PHP
$a = new stdClass();
$a->b = function() {};
$a->b(); // does not work "PHP Fatal error:  Call to undefined method stdClass::b() in Untitled.php on line 4"

$f = $a->b;
$f(); // works though

The same is true if closures are assigned to properties in class definitions:

class A {
    public $b;

    public function __construct() {
        $this->b = function() {};
    }

    public function c() {
        $this->b();
    }
}
$a = new A();
// neither
$a->b();
// nor
$a->c();
// do work

Fourth difference: JavaScript Closures are full fledged objects, wheres in PHP they are restricted objects. For instance, PHP Closures cannot have properties of their own:

$fn = function() {};
$fn->foo = 1;
// -> Catchable fatal error: Closure object cannot have properties

while in JavaScript you can do:

var fn = function() {};
fn.foo = 1;
fn.foo; // 1

Fifth difference: Returned closures can be immediately called upon in Javascript:

var fn = function() { return function() { alert('Hi');}}
fn()();    

Not in PHP:

$fn = function() { return function() { echo('Hi');};};
$fn()();     // syntax error

这篇关于Javascript闭包和PHP闭包,有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆