<?php
class Groot {
private $name;
private function __construct() {
$this->name = "I'm groot!";
}
public function getName() {
return $this->name;
}
public static function create($callback) {
// Cria uma nova instância da classe:
$new = new self();
// Associa a função ao objeto:
$callback = Closure::bind($callback, null, $new);
// Invoca a função anônima passando a instância como parâmetro:
$callback($new);
}
}
Groot::create(function ($self) {
echo $self->name, PHP_EOL;
});
PD9waHAKCmNsYXNzIEdyb290IHsKICAgIAogICAgcHJpdmF0ZSAkbmFtZTsKICAgIAogICAgcHJpdmF0ZSBmdW5jdGlvbiBfX2NvbnN0cnVjdCgpIHsKICAgICAgICAkdGhpcy0+bmFtZSA9ICJJJ20gZ3Jvb3QhIjsKICAgIH0KICAgIAogICAgcHVibGljIGZ1bmN0aW9uIGdldE5hbWUoKSB7CiAgICAgICAgcmV0dXJuICR0aGlzLT5uYW1lOwogICAgfQogICAgCiAgICBwdWJsaWMgc3RhdGljIGZ1bmN0aW9uIGNyZWF0ZSgkY2FsbGJhY2spIHsKICAgICAgICAKICAgICAgICAvLyBDcmlhIHVtYSBub3ZhIGluc3TDom5jaWEgZGEgY2xhc3NlOgogICAgICAgICRuZXcgPSBuZXcgc2VsZigpOwogICAgICAgIAogICAgICAgIC8vIEFzc29jaWEgYSBmdW7Dp8OjbyBhbyBvYmpldG86CiAgICAgICAgJGNhbGxiYWNrID0gQ2xvc3VyZTo6YmluZCgkY2FsbGJhY2ssIG51bGwsICRuZXcpOwogICAgICAgIAogICAgICAgIC8vIEludm9jYSBhIGZ1bsOnw6NvIGFuw7RuaW1hIHBhc3NhbmRvIGEgaW5zdMOibmNpYSBjb21vIHBhcsOibWV0cm86CiAgICAgICAgJGNhbGxiYWNrKCRuZXcpOwogICAgICAgIAogICAgfQogICAgCn0KCgpHcm9vdDo6Y3JlYXRlKGZ1bmN0aW9uICgkc2VsZikgewogICAgZWNobyAkc2VsZi0+bmFtZSwgUEhQX0VPTDsKfSk7