Last active
August 29, 2015 14:15
-
-
Save jeremeamia/63912686640adb402c6d to your computer and use it in GitHub Desktop.
PHP closure bindings... yeah, I know...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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