3v4l.org

run code in 300+ PHP versions simultaneously
<?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());
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Default value: string(3) "foo" Invoking the __constructor() method again: string(3) "bar" Closure binding: string(3) "baz" Abusing anonymous classes: string(4) "boom"
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Default value: string(3) "foo" Invoking the __constructor() method again: string(3) "bar" Closure binding: string(3) "baz" 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/fNTRa on line 4
Process exited with code 255.

preferences:
162.97 ms | 402 KiB | 182 Q