3v4l.org

run code in 300+ PHP versions simultaneously
<?php class HasValidToString { public function __toString() { return 'Object\'s __toString is valid'; } } class HasNonPublicToString { protected function __toString() { return 'has non public __toString'; } } class HasNullToStringReturnValue { public static function __toString() { } } class HasNumberReturnValue { public function __toString() { return 5; } } class HasObjectReturnValue { public function __toString() { return new HasValidToString(); } } // Do to PHP's fuzzy nature on how to handle things like this, you'll get a warning that // this method should not be static but it will happily let you call it on an object // instance so even though you might say that this is expected to fail it actually // won't. Chances are something else awful will happen, though. Short of dipping // into Reflection I'm not sure how to guard against this such that we do not // call into a static __toString method. class HasStaticToString { public static function __toString() { return 'Object\'s __toString is static; should not be able to call it'; } } $isExpectedToPass = true; $isExpectedToFail = false; function safely_cast_object_to_string($object) { if (! method_exists($object, '__toString')) { throw new InvalidArgumentException('Object has no __toString'); } if (! is_callable([$object, '__toString'])) { throw new InvalidArgumentException('Object\'s __toString is not callable'); } $string = $object->__toString(); if (! is_string($string)) { throw new InvalidArgumentException('Object\'s __toString does not return a string value'); } return $string; } foreach ([ [new HasValidToString(), $isExpectedToPass], [new HasNonPublicToString(), $isExpectedToFail], [new HasNullToStringReturnValue(), $isExpectedToFail], [new HasNumberReturnValue(), $isExpectedToFail], [new HasObjectReturnValue(), $isExpectedToFail], [new HasStaticToString(), $isExpectedToFail], // it actually passes... ] as $test) { list ($sample, $expectation) = $test; $actual = false; $string = null; $message = ''; try { $string = safely_cast_object_to_string($sample); $actual = true; } catch (\InvalidArgumentException $e) { $message = $e->getMessage(); } printf( "%26s [%s] %s\n", get_class($sample), $actual === $expectation ? 'PASS' : 'FAIL', ! is_null($string) ? $string : $message ); }

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.0070.01416.50
8.3.60.0140.00416.37
8.3.50.0060.01321.99
8.3.40.0090.00918.47
8.3.30.0070.00718.54
8.3.20.0040.00420.34
8.3.10.0050.00320.48
8.3.00.0070.00020.54
8.2.180.0100.01318.41
8.2.170.0040.01122.96
8.2.160.0090.00620.33
8.2.150.0040.00424.18
8.2.140.0090.00024.66
8.2.130.0080.00019.23
8.2.120.0080.00026.35
8.2.110.0000.00922.18
8.2.100.0090.00317.63
8.2.90.0040.00418.92
8.2.80.0000.00917.97
8.2.70.0030.00517.38
8.2.60.0000.00917.91
8.2.50.0090.00018.10
8.2.40.0070.00020.36
8.2.30.0040.00421.11
8.2.20.0040.00417.48
8.2.10.0040.00417.81
8.2.00.0040.00417.86
8.1.280.0100.00725.92
8.1.270.0000.00823.97
8.1.260.0040.00428.09
8.1.250.0040.00428.09
8.1.240.0000.00822.46
8.1.230.0040.00817.42
8.1.220.0000.00917.74
8.1.210.0030.00518.77
8.1.200.0030.00817.10
8.1.190.0040.00417.22
8.1.180.0040.00418.10
8.1.170.0060.00318.45
8.1.160.0050.00320.81
8.1.150.0040.00418.87
8.1.140.0020.00519.46
8.1.130.0060.00317.58
8.1.120.0030.00517.36
8.1.110.0000.00717.21
8.1.100.0040.00417.25
8.1.90.0040.00417.21
8.1.80.0040.00417.37
8.1.70.0000.00717.29
8.1.60.0030.00617.35
8.1.50.0030.00717.38
8.1.40.0000.00917.39
8.1.30.0040.00417.42
8.1.20.0030.00617.54
8.1.10.0000.00717.27
8.1.00.0040.00417.25
8.0.300.0000.00818.77
8.0.290.0040.00416.63
8.0.280.0000.00718.31
8.0.270.0000.00717.05
8.0.260.0060.00017.05
8.0.250.0000.00716.83
8.0.240.0030.00316.91
8.0.230.0030.00516.79
8.0.220.0070.00016.71
8.0.210.0040.00416.85
8.0.200.0060.00016.78
8.0.190.0030.00616.78
8.0.180.0040.00416.85
8.0.170.0000.00816.75
8.0.160.0040.00416.94
8.0.150.0040.00416.84
8.0.140.0040.00416.73
8.0.130.0000.00613.32
8.0.120.0000.00816.76
8.0.110.0000.00716.73
8.0.100.0050.00216.70
8.0.90.0000.00816.95
8.0.80.0100.00616.88
8.0.70.0040.00416.66
8.0.60.0000.00716.62
8.0.50.0040.00416.84
8.0.30.0090.00817.00
8.0.20.0080.01017.40
8.0.10.0090.00016.88
8.0.00.0100.00716.53
7.4.330.0060.00015.08
7.4.320.0000.00616.54
7.4.300.0000.00616.63
7.4.290.0000.00716.55
7.4.280.0000.00816.52
7.4.270.0070.00016.44
7.4.260.0030.00316.63
7.4.250.0040.00416.50
7.4.240.0000.00716.53
7.4.230.0000.00716.38
7.4.220.0030.01316.64
7.4.210.0140.00316.58
7.4.200.0070.00016.61
7.4.160.0060.01416.61
7.4.150.0070.01017.40
7.4.140.0150.00817.86
7.4.130.0070.01316.46
7.4.120.0080.01216.58
7.4.110.0130.00316.51
7.4.100.0030.01516.59
7.4.90.0100.01016.52
7.4.80.0140.00719.39
7.4.70.0080.00916.77
7.4.60.0130.00416.50
7.4.50.0100.00716.65
7.4.40.0060.01116.80
7.4.30.0090.00916.58
7.4.00.0070.00814.98
7.3.330.0000.00713.28
7.3.320.0000.00813.49
7.3.310.0000.00716.49
7.3.300.0030.00316.57
7.3.290.0090.00916.43
7.3.280.0080.00816.43
7.3.270.0120.00617.40
7.3.260.0100.00716.63
7.3.250.0100.01016.45
7.3.240.0130.00816.47
7.3.230.0100.00716.44
7.3.210.0060.01416.62
7.3.200.0060.01116.49
7.3.190.0110.00716.61
7.3.180.0080.01216.59
7.3.170.0130.00716.44
7.3.160.0030.01716.43
7.3.120.0070.01415.04
7.3.110.0060.01314.71
7.3.100.0040.01214.73
7.3.90.0040.00814.59
7.3.80.0070.01014.73
7.3.70.0000.01315.01
7.3.60.0030.00714.98
7.3.50.0090.00614.71
7.3.40.0000.01215.00
7.3.30.0030.01014.89
7.3.20.0110.00416.54
7.3.10.0080.00416.60
7.3.00.0070.01016.65
7.2.330.0150.00916.86
7.2.320.0090.00916.85
7.2.310.0030.01516.53
7.2.300.0130.00416.51
7.2.290.0060.01116.55
7.2.250.0120.00814.97
7.2.240.0160.00315.32
7.2.230.0090.00615.25
7.2.220.0000.01415.26
7.2.210.0030.00614.85
7.2.200.0060.01115.29
7.2.190.0030.00915.21
7.2.180.0030.00615.12
7.2.170.0060.00915.12
7.2.00.0110.01119.13
7.1.330.0060.00715.78
7.1.320.0000.01316.04
7.1.310.0030.01315.70
7.1.300.0030.00715.88
7.1.290.0070.00315.61
7.1.280.0040.00815.59
7.1.270.0040.01115.77
7.1.260.0030.01016.02
7.1.100.0040.00718.05
7.1.70.0000.00817.23
7.1.60.0100.01319.12
7.1.00.0070.07322.57
7.0.200.0030.00616.78
7.0.60.0270.07719.89
7.0.50.0030.05717.89
7.0.40.0070.05720.30
7.0.30.0230.07320.30
7.0.20.0100.04720.34
7.0.10.0100.08320.07
7.0.00.0070.04020.33
5.6.280.0000.07321.02
5.6.210.0070.07720.51
5.6.200.0070.03718.22
5.6.190.0070.05020.39
5.6.180.0030.08720.50
5.6.170.0300.06020.44
5.6.160.0100.08020.71
5.6.150.0170.06018.29
5.6.140.0070.07318.35
5.6.130.0100.06318.28
5.6.120.0130.08321.05
5.6.110.0000.08320.92
5.6.100.0070.08721.04
5.6.90.0100.06321.00
5.6.80.0030.04320.39
5.5.350.0230.04320.49
5.5.340.0000.04718.08
5.5.330.0100.07020.42
5.5.320.0330.07020.52
5.5.310.0230.03720.27
5.5.300.0000.04317.99
5.5.290.0130.08317.97
5.5.280.0100.07720.76
5.5.270.0170.05720.82
5.5.260.0100.06720.91
5.5.250.0070.08020.71
5.5.240.0170.06320.19

preferences:
57.84 ms | 401 KiB | 5 Q