- var_dump: documentation ( source)
<?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());
// ------------------------- Method 1 -------------------------
echo "\nInvoking the __constructor() method again:\n";
$foo->__construct("bar");
var_dump($foo->getProperty());
// ------------------------- Method 2 -------------------------
// https://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/
echo "\nClosure binding:\n";
Closure::bind(
function (): void {
$this->property = "baz";
},
$foo,
$foo
)->__invoke();
var_dump($foo->getProperty());
// ------------------------- Method 3 -------------------------
echo "\nAbusing anonymous classes:\n";
$fooAbuser = new class("") extends Foo
{
public function abuse(Foo $foo): void
{
$foo->property = "boom";
}
};
$fooAbuser->abuse($foo);
var_dump($foo->getProperty());