@ 2020-02-21T09:50:46Z <?php
class Foo {
protected string $property;
public function __construct(string $property)
{
$this->property = $property;
}
public function getProperty(): string
{
return $this->property;
}
}
$foo = new Foo("foo");
echo "Default value:\n";
var_dump($foo->getProperty());
echo "\nInvoking the __constructor() method again:\n";
$foo->__construct("baz");
var_dump($foo->getProperty());
// https://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/
echo "\nClosure binding:\n";
Closure::bind(
function (): void {
$this->property = "bang";
},
$foo,
$foo
)->__invoke();
var_dump($foo->getProperty());
echo "\nAbusing anonymous classes:\n";
$fooAbuser = new class("") extends Foo
{
public function abuse(Foo $foo): Foo
{
$foo->property = "boom";
return $foo;
}
};
var_dump($fooAbuser->abuse($foo)->getProperty());
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.33 , 8.2.0 - 8.2.29 , 8.3.0 - 8.3.25 , 8.4.1 - 8.4.12 Default value:
string(3) "foo"
Invoking the __constructor() method again:
string(3) "baz"
Closure binding:
string(4) "bang"
Abusing anonymous classes:
string(4) "boom"
Output for 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 Parse error: syntax error, unexpected 'string' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/Ta4PM on line 4
Process exited with code 255 . preferences:dark mode live preview ace vim emacs key bindings
140.24 ms | 407 KiB | 5 Q