Giới thiệu PHP và JavaScript Closures, cài đặt Drupal 8

Giới thiệu PHP và JavaScript Closures, cài đặt Drupal 8

PHP closures

PHP closures are pretty simple as they are barely more than syntactic sugar over the following:

class Something {
  function __construct($x) {
    $this->x = $x;
  }
  function __invoke($y) {
    extract(get_object_vars($this));
    // Your closure here.
  }
}

Becomes:

function something ($y) use ($x) {
  // Your closure here.
}

So closures are objects with a small difference: they are automatically constructed and once constructed they can not be changed and the only thing you can do with them is call them. Now it should be easy to see how variables work: variables given in use() are copied to properties on the object. If $x is an object itself then of course only its handler is copied so changing from inside the closure affects it everywhere else, exactly like how objects work in any other operation. All this is quite consistent on how PHP works and relatively simple to understand.

function foo() {
  $x = 1;
  $y = function() use (&$x) {
    $x++;
    print "in $x\n";
  };
  $y();
  print "$x\n";
  return $y;
}
$func = foo();
$func();
print "$x\n";

JavaScript

JavaScript is just a little different. First of all, there is no explicit import, every variable from the parent scope is imported. Second, since everything is an object, changing these variables affects the variables in the parent scope.

function foo() {
  var x = 1;
  var y = function() { x++; console.log('in' + x);}
  y();
  console.log(x);
  return y;
}
func = foo();
func();
console.log(x);

Flow Control

In both languages returning from a closure will simply return to the caller. If the closure is called in a loop then the loop will continue. Short of throwing an exception the closure can’t stop such a loop. See Smalltalk for an example of a language where this is different. Obviously, Common Lisp can do both kinds of returns and the syntax is succinct and easy to understand. Obviously again, Ruby can do both and the syntax is extremely obscure.

About $this / this

Since PHP 5.4, you can use $this in closure. Just imagine that one is passed in via use() and everything will be fine. So $this always means the object it is defined in even if the closure is passed to another method on another object. If necessary then a new closure can be created with a new $this variable: Closure::bind($closure, $newthis) or $closure->bindTo($newthis):

class foo {
  protected $x = 1;
  function bar() {
    return function() { 
      $this->x++; 
      print "$this->x\n";};
  }
}
$func = (new foo)->bar();
$func();
class bar {
  protected $x = 10;
}
$func2 = $func->bindTo(new bar, "bar"); // "bar" allows the closure to access protected things
$func2();

JavaScript this means the defining scope however it can be changed when calling the closures via the call or apply methods of the closure. This doesn't have a PHP equivalent. Your favorite framework or native DOM handling will often do this for you. each in jQuery sets this to the current object, event handlers will get the current event in this etc. ES5.1 in 2011 introduced the bind method on function objects which behaves exactly like bindTo in PHP: something.bind(newThis) returns a new closure with this being set to newThis. Examples:

function foo() {
  x = 1;
  var y = function() { this.x++; console.log(this.x);}
  y();
  return y;
}
var func = foo();
func();
var func = function() { this.x++; console.log(this.x);}
func.call({x:1});
o = {x:10};
var func2 = func.bind(o);
func2();
Bạn thấy bài viết này như thế nào?: 
Average: 5 (1 vote)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
ASUS Transformer Prime Tablet: First Tab With Nvidia Tegra 3 quad-core CPU

ASUS Transformer Prime Tablet: First Tab With Nvidia Tegra 3 quad-core CPU

ASUS is taking its work seriously. Just after seven months of releasing the ASUS Eee Pad Transformer, its successor the ASUS Transformer Prime is out in the market with a heavy dose of addicting functions.

Tự tạo trang đăng nhập cực đẹp dành cho Facebook

Tự tạo trang đăng nhập cực đẹp dành cho Facebook

Đôi khi bạn cảm thấy nhàm chán với giao diện đăng nhập của Facebook và muốn thay đổi chúng. Hướng dẫn dưới đây sẽ giúp bạn giải quyết vấn đề đó.

Bạn đã thật sự bắt đầu cho Drupal 8?

Bạn đã thật sự bắt đầu cho Drupal 8?

You may not be able to control when Drupal 8 is ready. But you can control when you are ready for Drupal 8. Attending DrupalCon Austin is a great way to start.

Công ty diệt chuột T&C

 

Diet con trung