<?php
class Foo {
public function __construct($prop) {
$this->prop = $prop;
}
public function __toString() {
return $this->prop;
}
}
class MyClass
{
public function __construct()
{
$this->prop = new Foo("test");
}
public function test()
{
$class = new class($this->prop) {
public function __construct($prop)
{
$this->prop = $prop;
}
public function run()
{
$this->prop->prop = "Teste na classe anĂ´nima";
echo $this->prop, PHP_EOL;
}
};
$class->run();
}
}
$obj = new MyClass();
$obj->test();
echo $obj->prop, PHP_EOL;
PD9waHAKCmNsYXNzIEZvbyB7CiAgICBwdWJsaWMgZnVuY3Rpb24gX19jb25zdHJ1Y3QoJHByb3ApIHsKICAgICAgICAkdGhpcy0+cHJvcCA9ICRwcm9wOwogICAgfQogICAgCiAgICBwdWJsaWMgZnVuY3Rpb24gX190b1N0cmluZygpIHsKICAgICAgICByZXR1cm4gJHRoaXMtPnByb3A7CiAgICB9Cn0KCmNsYXNzIE15Q2xhc3MKewogICAgcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0KCkKICAgIHsKICAgICAgICAkdGhpcy0+cHJvcCA9IG5ldyBGb28oInRlc3QiKTsKICAgIH0KCiAgICBwdWJsaWMgZnVuY3Rpb24gdGVzdCgpCiAgICB7CiAgICAgICAgJGNsYXNzID0gbmV3IGNsYXNzKCR0aGlzLT5wcm9wKSB7CgogICAgICAgICAgICBwdWJsaWMgZnVuY3Rpb24gX19jb25zdHJ1Y3QoJHByb3ApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICR0aGlzLT5wcm9wID0gJHByb3A7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIHB1YmxpYyBmdW5jdGlvbiBydW4oKSAKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgJHRoaXMtPnByb3AtPnByb3AgPSAiVGVzdGUgbmEgY2xhc3NlIGFuw7RuaW1hIjsKICAgICAgICAgICAgICAgIGVjaG8gJHRoaXMtPnByb3AsIFBIUF9FT0w7CiAgICAgICAgICAgIH0KCiAgICAgICAgfTsKICAgICAgICAKICAgICAgICAkY2xhc3MtPnJ1bigpOwogICAgfQp9Cgokb2JqID0gbmV3IE15Q2xhc3MoKTsKJG9iai0+dGVzdCgpOwoKZWNobyAkb2JqLT5wcm9wLCBQSFBfRU9MOw==