Skip to content

Instantly share code, notes, and snippets.

@jeremeamia
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremeamia/63912686640adb402c6d to your computer and use it in GitHub Desktop.
Save jeremeamia/63912686640adb402c6d to your computer and use it in GitHub Desktop.
PHP closure bindings... yeah, I know...
<?php
// Require composer autoloader after installing superclosure 2.0.
require '/vendor/autoload.php';
class ClosureCreator
{
public function getClosures()
{
$f1 = function () {};
$f2 = static function () {};
$f3 = function () {}; $f3 = $f3->bindTo(null);
$f4 = function () {}; $f4 = $f4->bindTo(null, null);
return [$f1, $f2, $f3, $f4];
}
}
// Set up array of closure tests cases.
$closures = (new ClosureCreator)->getClosures();
$f0 = function () {};
array_unshift($closures, $f0);
// Analyze each closure for their binding, scope, and staticness.
$analyzer = new \SuperClosure\Analyzer\AstAnalyzer();
foreach ($closures as $i => $closure) {
$data = $analyzer->analyze($closure);
echo "f{$i}:\n";
echo "\tBinding: " . (is_object($data['binding']) ? get_class($data['binding']) : 'NULL') . "\n";
echo "\tScope: " . ($data['scope'] ?: 'NULL') . "\n";
echo "\tStatic: " . ($data['isStatic'] ? 'YES' : 'NO') . "\n";
}
f0:
Binding: NULL
Scope: NULL
Static: NO
f1:
Binding: ClosureCreator
Scope: ClosureCreator
Static: NO
f2:
Binding: NULL
Scope: ClosureCreator
Static: YES
f3:
Binding: NULL
Scope: ClosureCreator
Static: YES
f4:
Binding: NULL
Scope: NULL
Static: NO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment