<?php
class Groot {
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();
// Invoca a função anônima passando a instância como parâmetro:
$callback($new);
}
}
Groot::create(function ($self) {
echo $self->getName(), PHP_EOL;
});
PD9waHAKCmNsYXNzIEdyb290IHsKICAgIAogICAgcHJpdmF0ZSBmdW5jdGlvbiBfX2NvbnN0cnVjdCgpIHsKICAgICAgICAkdGhpcy0+bmFtZSA9ICJJJ20gZ3Jvb3QhIjsKICAgIH0KICAgIAogICAgcHVibGljIGZ1bmN0aW9uIGdldE5hbWUoKSB7CiAgICAgICAgcmV0dXJuICR0aGlzLT5uYW1lOwogICAgfQogICAgCiAgICBwdWJsaWMgc3RhdGljIGZ1bmN0aW9uIGNyZWF0ZSgkY2FsbGJhY2spIHsKICAgICAgICAKICAgICAgICAvLyBDcmlhIHVtYSBub3ZhIGluc3TDom5jaWEgZGEgY2xhc3NlOgogICAgICAgICRuZXcgPSBuZXcgc2VsZigpOwogICAgICAgIAogICAgICAgIC8vIEludm9jYSBhIGZ1bsOnw6NvIGFuw7RuaW1hIHBhc3NhbmRvIGEgaW5zdMOibmNpYSBjb21vIHBhcsOibWV0cm86CiAgICAgICAgJGNhbGxiYWNrKCRuZXcpOwogICAgICAgIAogICAgfQogICAgCn0KCgpHcm9vdDo6Y3JlYXRlKGZ1bmN0aW9uICgkc2VsZikgewogICAgZWNobyAkc2VsZi0+Z2V0TmFtZSgpLCBQSFBfRU9MOwp9KTs=