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?
A closure is an anonymous function that can access variables imported from the outside scope without using any global variables. Theoretically, a closure is a function with some arguments closed (e.g. fixed) by the environment when it is defined. Closures can work around variable scope restrictions in a clean way.
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.
In JavaScript, closures are the primary mechanism used to enable data privacy. When you use closures for data privacy, the enclosed variables are only in scope within the containing (outer) function. You can't get at the data from an outside scope except through the object's privileged methods.
a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables. a callback is executable code that is passed as an argument to other code.
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
The only thing I've found in PHP (that is totally cool and really handy!) is the ability to use them as getters and setters in classes which was always a nightmare to achieve before, JavaScript can be used in the same way but they do both act almost identically from what I've seen.
I'm not sure about the namespacing convention differences between the two but as @Rijk pointed out there is a section on the PHP website dedicated to them
<?php class testing { private $foo = 'Hello '; public $bar = 'Bar'; #Act like a getter and setter! public static $readout = function ($val = null) { if (!empty($val)) { testing::$readout = $val; } return testing::$readout; } }
They are also really great for...
Looping through items with a controller rather than a new for/each loop on the page
Great for supplying as arguments to functions/classes
Whats annoying about them is...
You can't typecast them, since they're just functions...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With