3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sql(callable $expr): array { if ($context = (new \ReflectionFunction($expr))->getClosureThis()) { $placeholder = new class($context) { private object $context; private ReflectionObject $reflection; public function __construct(object $context) { $this->context = $context; $this->reflection = new \ReflectionObject($context); } public function __call($method, $args) { $method = $this->reflection->getMethod($method); return $method->getClosure($this->context)->call($this->context, ...$args); } public function __get($property) { $property = $this->reflection->getProperty($property); $property->setAccessible(true); return $property->getValue($this->context); } public function __isset($property): bool { return $this->reflection->hasProperty($property); } public function __set($property, $value): void { $property = $this->reflection->getProperty($property); $property->setAccessible(true); $property->setValue($this->context, $value); } public function __toString(): string { return '?'; } }; } else { $placeholder = new class { public function __toString(): string { return '?'; } }; } $expr = Closure::fromCallable($expr) ->bindTo($placeholder, $placeholder); $params = []; $generator = $expr(); while ($generator->valid()) { $params[] = $generator->current(); // Get the value from "yield" $generator->send('this'); // Insert placeholder } return [$generator->getReturn(), $params]; } class Foo { private int $id = 42; public function bar(): void { [$query, $params] = sql(fn() => "SELECT * FROM users WHERE id = ${yield $this->id} OR username = ${yield $this->username()}"); var_dump( $query, $params, ); } private function username(): string { return 'azjezz'; } } $f = new Foo(); $f->bar();
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.4, 8.3.6 - 8.3.26, 8.4.1 - 8.4.13
Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in /in/h00Uq on line 71 Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in /in/h00Uq on line 71 string(48) "SELECT * FROM users WHERE id = ? OR username = ?" array(2) { [0]=> int(42) [1]=> string(6) "azjezz" }
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 Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in /in/h00Uq on line 71 Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in /in/h00Uq on line 71 string(48) "SELECT * FROM users WHERE id = ? OR username = ?" array(2) { [0]=> int(42) [1]=> string(6) "azjezz" }
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33
string(48) "SELECT * FROM users WHERE id = ? OR username = ?" array(2) { [0]=> int(42) [1]=> string(6) "azjezz" }
Output for 7.3.0 - 7.3.33
Parse error: syntax error, unexpected 'object' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/h00Uq on line 7
Process exited with code 255.

preferences:
105.31 ms | 410 KiB | 5 Q