3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Orig1 { public static function test() { echo "ok1\n"; } } class Orig2 { public static function test() { echo "ok2\n"; } } class Delegator { public static function delegate(object $orig) { static $cache = []; $origClass = get_class($orig); if (!isset($cache[$origClass])) { $className = 'Delegate__' . count($cache); $cache[$origClass] = $className; eval(" class $className { public static function __callStatic(\$name, \$args) { return $origClass::\$name(...\$args); } } "); } return new $cache[$origClass]($orig); } } $o1 = new Orig1(); $d1 = Delegator::delegate($o1); $o2 = new Orig2(); $d2 = Delegator::delegate($o2); $d1::test(); $d2::test(); $d1::test();
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
ok1 ok2 ok1

preferences:
85.56 ms | 1234 KiB | 4 Q