3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A { protected $c; protected $d; public function set(B $test) { static $testMap = []; if (empty($testMap)) { $testMap = [ C::class => 'setC', // only need to know which method to call D::class => 'setD', // ]; } if ($test === null) { return; } $testType = get_class($test); if (!array_key_exists($testType, $testMap)) { throw new InvalidArgumentException( sprintf( "Encountered unexpected body type (%s) that isn't supported", $testType ) ); } call_user_func([$this, $testMap[$testType]], $test); // uses $this to call the method on this object // alternatively, $this->{$testMap[$testType]}($test); } /** * @param C|null $c */ public function setC(C $c = null) { $this->c = $c; } /** * @param D|null $d */ public function setD(D $d = null) { $this->d = $d; } } abstract class B { protected $data; public function __construct($q = '') { $this->data = $q; } } class C extends B { } class D extends B {} $a1 = new A; $a1->set(new C("test c1")); var_dump($a1); $a2 = new A; $a2->set(new C("test c2")); var_dump($a2); $a3 = new A; $a3->set(new C("test c3")); var_dump($a3);

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.70.0130.00318.54
8.3.60.0100.01018.55
8.3.50.0070.00718.29
8.3.40.0090.00920.21
8.3.30.0100.00718.76
8.3.20.0040.00421.98
8.3.10.0040.00422.10
8.3.00.0050.00222.57
8.2.190.0000.01816.58
8.2.180.0120.00316.63
8.2.170.0110.00422.96
8.2.160.0100.00619.46
8.2.150.0080.00024.18
8.2.140.0000.00824.66
8.2.130.0030.00526.16
8.2.120.0050.00326.16
8.2.110.0100.00019.32
8.2.100.0090.00317.75
8.2.90.0000.00819.14
8.2.80.0040.00417.97
8.2.70.0030.00617.50
8.2.60.0040.00417.61
8.2.50.0080.00018.05
8.2.40.0060.00319.88
8.2.30.0020.00518.04
8.2.20.0040.00418.18
8.2.10.0040.00419.42
8.2.00.0000.00819.45
8.1.280.0090.00625.92
8.1.270.0040.00422.02
8.1.260.0000.00726.35
8.1.250.0060.00328.09
8.1.240.0100.00023.97
8.1.230.0090.00317.77
8.1.220.0000.00817.74
8.1.210.0040.00418.77
8.1.200.0040.00817.66
8.1.190.0030.00517.23
8.1.180.0050.00318.10
8.1.170.0000.00817.62
8.1.160.0050.00218.94
8.1.150.0000.00718.92
8.1.140.0000.00718.98
8.1.130.0000.00720.17
8.1.120.0040.00417.39
8.1.110.0000.00817.38
8.1.100.0080.00017.52
8.1.90.0040.00417.40
8.1.80.0080.00017.54
8.1.70.0070.00017.48
8.1.60.0040.00417.60
8.1.50.0000.00817.52
8.1.40.0000.00817.41
8.1.30.0050.00317.63
8.1.20.0050.00217.58
8.1.10.0040.00417.64
8.1.00.0000.00817.64
8.0.300.0050.00218.77
8.0.290.0040.00416.75
8.0.280.0030.00318.39
8.0.270.0030.00318.11
8.0.260.0050.00321.77
8.0.250.0030.00416.94
8.0.240.0060.00317.02
8.0.230.0070.00016.96
8.0.220.0000.00716.97
8.0.210.0000.00717.01
8.0.200.0000.00617.06
8.0.190.0050.00216.97
8.0.180.0030.00516.99
8.0.170.0050.00316.98
8.0.160.0000.00817.04
8.0.150.0050.00216.94
8.0.140.0070.00016.92
8.0.130.0000.00613.46
8.0.120.0040.00416.93
8.0.110.0050.00216.84
8.0.100.0040.00417.03
8.0.90.0000.00916.91
8.0.80.0130.00717.00
8.0.70.0000.00716.88
8.0.60.0040.00416.82
8.0.50.0000.00716.88
8.0.30.0030.01717.03
8.0.20.0150.00917.19
8.0.10.0000.00817.00
8.0.00.0090.00916.55
7.4.330.0060.00015.55
7.4.320.0000.00616.65
7.4.300.0000.00716.59
7.4.290.0040.00416.54
7.4.280.0070.00016.53
7.4.270.0040.00416.54
7.4.260.0000.00513.43
7.4.250.0080.00016.43
7.4.240.0040.00416.65
7.4.230.0000.00716.54
7.4.220.0020.00516.62
7.4.210.0100.00616.62
7.4.200.0070.00016.34
7.4.130.0120.00716.43
7.4.120.0110.00716.61
7.4.110.0110.00716.55
7.4.100.0070.01116.52
7.4.90.0140.00716.79
7.4.80.0070.01419.39
7.4.70.0090.00916.70
7.4.60.0140.01016.43
7.4.50.0000.01216.55
7.4.40.0040.01416.52
7.4.30.0070.01316.46
7.4.20.0100.01016.45
7.4.10.0130.00816.50
7.4.00.0130.01316.71
7.3.330.0040.00416.20
7.3.320.0000.00513.46
7.3.310.0080.00016.41
7.3.300.0080.00016.25
7.3.290.0060.01116.35
7.3.260.0140.01116.37
7.3.230.0150.00416.32
7.3.210.0150.00316.53
7.3.200.0150.00416.31
7.3.190.0030.01316.63
7.3.180.0100.00716.72
7.3.170.0060.01116.46
7.3.160.0100.00716.47
7.3.150.0040.01816.50
7.3.140.0100.01016.41
7.3.130.0130.01016.31
7.3.120.0060.01216.37
7.3.110.0130.00616.47
7.3.100.0190.00616.46
7.3.90.0070.01116.27
7.3.80.0060.01616.37
7.3.70.0160.00316.47
7.3.60.0140.00516.40
7.3.50.0160.00316.46
7.3.40.0120.00916.46
7.3.30.0120.00616.34
7.3.20.0180.00916.36
7.3.10.0150.01116.49
7.3.00.0070.01016.39
7.2.330.0070.01316.50
7.2.320.0100.00716.49
7.2.310.0060.01016.61
7.2.300.0130.00316.54
7.2.290.0060.01216.90
7.2.280.0120.01216.54
7.2.270.0140.01116.46
7.2.260.0150.00916.84
7.2.250.0150.00616.58
7.2.240.0250.01616.43
7.2.230.0060.01516.56
7.2.220.0120.00916.59
7.2.210.0080.01516.86
7.2.200.0040.01616.52
7.2.190.0100.01316.77
7.2.180.0120.01216.77
7.2.170.0070.01016.60
7.2.160.0050.01616.61
7.2.150.0130.00716.75
7.2.140.0160.00416.70
7.2.130.0130.01016.73
7.2.120.0150.00916.72
7.2.110.0110.01216.77
7.2.100.0100.01016.63
7.2.90.0140.01416.85
7.2.80.0170.00316.57
7.2.70.0150.00616.76
7.2.60.0090.01216.59
7.2.50.0080.01116.74
7.2.40.0130.00916.73
7.2.30.0080.01216.59
7.2.20.0000.01916.43
7.2.10.0060.01316.64
7.2.00.0060.01616.85

preferences:
78.1 ms | 401 KiB | 5 Q