Reputation: 35194
$that = $this;
$closure = function (...) use ($that) { ... };
Found on: http://css.dzone.com/polls/what-new-feature-php-54
Could someone please explain what it does? Preferably with an example.
Upvotes: 3
Views: 504
Reputation: 5397
that is a way to have access to the methods of a class from a anonymous function defined in it. but since the title is "php 5.4 closures" you don't need to do that any more, this is one of the updates made in 5.4, you can use $this (without passing it to another variable like $that). you can see here http://php.net/ChangeLog-5.php that one of the changes is "Added closure $this support back"
Upvotes: 1
Reputation: 237
A closure is an anonymous function, often used with callbacks. For example:
my_function_with_callback('some-parameter', function() {
//Do stuff here
});
The rebinding means that you can use $this in an anomymous function, instead of having to use: 'use($var)', so when you're in a class:
class MyClass {
public function myMethod() {
$anon = function() {
//$this still refers to MyClass here
};
}
}
Hope this answers your question
Upvotes: 0