<?php
$obj = new class {
public function __construct ()
{
$this->key1 = new stdClass();
$this->key1->var1 = "SOpt";
}
public function getKey1 ()
{
return $this->key1;
}
public function __toString ()
{
return "Objeto criado com classe anônima";
}
};
// Acessando o atributo diretamente:
echo $obj->key1->var1, PHP_EOL;
// Acessando o atributo através do método get:
echo $obj->getKey1()->var1, PHP_EOL;
// Chamando o método __toString do objeto:
echo $obj, PHP_EOL;
// Exibindo a classe do objeto:
PD9waHAKCiRvYmogPSBuZXcgY2xhc3MgewogICAgcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0ICgpCiAgICB7CiAgICAgICAgJHRoaXMtPmtleTEgPSBuZXcgc3RkQ2xhc3MoKTsKICAgICAgICAkdGhpcy0+a2V5MS0+dmFyMSA9ICJTT3B0IjsKICAgIH0KICAgIAogICAgcHVibGljIGZ1bmN0aW9uIGdldEtleTEgKCkKICAgIHsKICAgICAgICByZXR1cm4gJHRoaXMtPmtleTE7CiAgICB9CiAgICAKICAgIHB1YmxpYyBmdW5jdGlvbiBfX3RvU3RyaW5nICgpCiAgICB7CiAgICAgICAgcmV0dXJuICJPYmpldG8gY3JpYWRvIGNvbSBjbGFzc2UgYW7DtG5pbWEiOwogICAgfQp9OwoKLy8gQWNlc3NhbmRvIG8gYXRyaWJ1dG8gZGlyZXRhbWVudGU6CmVjaG8gJG9iai0+a2V5MS0+dmFyMSwgUEhQX0VPTDsKCi8vIEFjZXNzYW5kbyBvIGF0cmlidXRvIGF0cmF2w6lzIGRvIG3DqXRvZG8gZ2V0OgplY2hvICRvYmotPmdldEtleTEoKS0+dmFyMSwgUEhQX0VPTDsKCi8vIENoYW1hbmRvIG8gbcOpdG9kbyBfX3RvU3RyaW5nIGRvIG9iamV0bzoKZWNobyAkb2JqLCBQSFBfRU9MOwoKLy8gRXhpYmluZG8gYSBjbGFzc2UgZG8gb2JqZXRvOgplY2hvIGdldF9jbGFzcygkb2JqKSwgUEhQX0VPTDs=