3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Sealed class can not be created directly */ class Seal { private function __construct() { // private ctor prevents from direct creation of instance } } #1 Via traditional reflection without constructor $refClass = new ReflectionClass(Seal::class); $instance = $refClass->newInstanceWithoutConstructor(); var_dump($instance); #2 Via closure binding to the sealed class $instantinator = function () { return new static; }; $instantinator = $instantinator->bindTo(null, Seal::class); $instance = $instantinator(); var_dump($instance); #3 Via unserialize hack $instance = unserialize(sprintf('O:%d:"%s":0:{}', strlen(Seal::class), Seal::class)); var_dump($instance); #4 Via PDO, requires pdo_sqlite which is typically available, can be pdo_mysql, then change: SELECT "test" FROM DUAL if (phpversion('pdo_sqlite')) { $database = new PDO('sqlite::memory:'); $result = $database->query('SELECT "test" as field1'); // We can even initialize properties in the object $instance = $result->fetchObject(Seal::class); var_dump($instance); } #5 Via STOMP, requires php_stomp to be loaded if (phpversion('stomp')) { $connection = new Stomp(/* connection args */); $connection->subscribe('Test'); $connection->readFrame(Seal::class); }
Output for 5.6.38 - 5.6.40, 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.25, 7.3.0 - 7.3.12, 7.4.0
object(Seal)#2 (0) { } object(Seal)#3 (0) { } object(Seal)#2 (0) { } object(Seal)#6 (1) { ["field1"]=> string(4) "test" }

preferences:
118.74 ms | 402 KiB | 83 Q