3v4l.org

run code in 300+ PHP versions simultaneously
<?php // // The goal is to try to safely cast an object to string but only if it can reasonably // be assumed to be stringable. (properly implements __toString) // // This is as close as I've been able to come to ensure that bad things do not get // called and I only deal with proper string results. // // The only major edge case here is that without implementing reflection it is // impossible to know if __toString was implemented as a static method. This // is a bummer but I'm willing to call this one "good enough." // 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.0170.01718.34
8.3.60.0150.00816.63
8.3.50.0160.00222.07
8.3.40.0040.01518.70
8.3.30.0070.00718.62
8.3.20.0050.00220.16
8.3.10.0050.00320.43
8.3.00.0040.00420.78
8.2.190.0070.00716.88
8.2.180.0110.00418.37
8.2.170.0110.00422.96
8.2.160.0110.00420.44
8.2.150.0040.00724.18
8.2.140.0040.00424.66
8.2.130.0070.00026.16
8.2.120.0070.00418.93
8.2.110.0070.00319.39
8.2.100.0110.00017.78
8.2.90.0030.00617.75
8.2.80.0000.00818.61
8.2.70.0050.00317.38
8.2.60.0000.00817.50
8.2.50.0060.00317.55
8.2.40.0030.00619.12
8.2.30.0000.00817.99
8.2.20.0040.00417.45
8.2.10.0000.00717.87
8.2.00.0000.00917.82
8.1.280.0040.01125.92
8.1.270.0040.00420.80
8.1.260.0060.00326.35
8.1.250.0080.00028.09
8.1.240.0050.00522.66
8.1.230.0040.00720.82
8.1.220.0000.00717.74
8.1.210.0030.00518.77
8.1.200.0030.00617.25
8.1.190.0000.00917.23
8.1.180.0040.00418.10
8.1.170.0040.00419.02
8.1.160.0000.00818.73
8.1.150.0040.00418.74
8.1.140.0000.00719.45
8.1.130.0070.00017.58
8.1.120.0000.00817.39
8.1.110.0040.00417.23
8.1.100.0000.00917.38
8.1.90.0040.00417.31
8.1.80.0040.00417.35
8.1.70.0000.00817.24
8.1.60.0000.00817.43
8.1.50.0060.00617.38
8.1.40.0040.00417.43
8.1.30.0090.00017.51
8.1.20.0040.00417.52
8.1.10.0050.00317.40
8.1.00.0040.00417.35
8.0.300.0040.00418.99
8.0.290.0040.00416.63
8.0.280.0030.00618.25
8.0.270.0070.00017.05
8.0.260.0000.00817.08
8.0.250.0040.00416.79
8.0.240.0030.00316.89
8.0.230.0000.00816.94
8.0.220.0030.00316.82
8.0.210.0000.00716.73
8.0.200.0000.00716.92
8.0.190.0030.00616.84
8.0.180.0040.00416.79
8.0.170.0080.00016.77
8.0.160.0000.00816.85
8.0.150.0000.00816.80
8.0.140.0020.00516.73
8.0.130.0030.00313.22
8.0.120.0040.00416.80
8.0.110.0040.00416.83
8.0.100.0070.00016.71
8.0.90.0000.00716.81
8.0.80.0030.01316.73
8.0.70.0070.00016.73
8.0.60.0040.00416.67
8.0.50.0040.00416.66
8.0.30.0060.01016.91
8.0.20.0130.00817.40
8.0.10.0030.00616.88
8.0.00.0080.01116.76
7.4.330.0050.00015.08
7.4.320.0000.00616.70
7.4.300.0030.00316.57
7.4.290.0000.00716.56
7.4.280.0040.00416.54
7.4.270.0000.00716.54
7.4.260.0080.00016.55
7.4.250.0070.00016.48
7.4.240.0070.00016.61
7.4.230.0030.00316.54
7.4.220.0060.01416.51
7.4.210.0030.01016.49
7.4.200.0070.00016.45
7.4.160.0080.01216.38
7.4.150.0090.00917.40
7.4.140.0090.01417.86
7.4.130.0090.00816.73
7.4.120.0100.00916.54
7.4.110.0150.00416.71
7.4.100.0100.00716.59
7.4.90.0060.01116.66
7.4.80.0000.01719.39
7.4.70.0100.01316.53
7.4.60.0070.01116.45
7.4.50.0060.00916.28
7.4.40.0090.00916.53
7.4.30.0070.01116.60
7.4.00.0100.00715.25
7.3.330.0000.00913.51
7.3.320.0040.00413.42
7.3.310.0070.00016.56
7.3.300.0000.00716.34
7.3.290.0070.00916.49
7.3.280.0080.01116.46
7.3.270.0090.00917.40
7.3.260.0140.00316.41
7.3.250.0090.00916.57
7.3.240.0090.00816.50
7.3.230.0110.00716.59
7.3.210.0030.01316.65
7.3.200.0100.00716.57
7.3.190.0060.01816.32
7.3.180.0100.01016.38
7.3.170.0060.01616.59
7.3.160.0100.00716.39
7.3.00.0090.00316.75
7.2.330.0140.00316.86
7.2.320.0090.00916.52
7.2.310.0030.01516.73
7.2.300.0090.01016.65
7.2.290.0090.00916.84
7.2.130.0110.00316.92
7.2.120.0060.00616.74
7.2.110.0000.01517.09
7.2.100.0040.00417.06
7.2.90.0070.00717.08
7.2.80.0030.00616.99
7.2.70.0110.00716.82
7.2.60.0180.00217.04
7.2.50.0040.01416.99
7.2.40.0080.00517.02
7.2.30.0110.00817.08
7.2.20.0210.00016.99
7.2.10.0100.00616.91
7.2.00.0060.00916.93
7.1.250.0000.01215.90
7.1.240.0000.01415.63
7.1.230.0030.00715.77
7.1.220.0060.00315.79
7.1.210.0070.00715.66
7.1.200.0080.00715.72
7.1.190.0030.00715.91
7.1.180.0030.00715.77
7.1.170.0060.00315.89
7.1.160.0040.00815.98
7.1.150.0040.00715.67
7.1.140.0040.01415.88
7.1.130.0070.00715.65
7.1.120.0100.00315.75
7.1.110.0030.01015.82
7.1.100.0130.00315.61
7.1.90.0030.00615.94
7.1.80.0030.00615.95
7.1.70.0100.00416.51
7.1.60.0080.01017.37
7.1.50.0080.00315.93
7.1.40.0000.00915.69
7.1.30.0130.00315.66
7.1.20.0030.00715.53
7.1.10.0090.00615.85
7.1.00.0030.04019.23
7.0.330.0120.00615.53
7.0.320.0070.00715.38
7.0.310.0000.01015.18
7.0.300.0080.00815.42
7.0.290.0070.00714.99
7.0.280.0150.00015.42
7.0.270.0030.01015.54
7.0.260.0150.00015.46
7.0.250.0030.00615.48
7.0.240.0000.00915.44
7.0.230.0070.00715.12
7.0.220.0070.00715.37
7.0.210.0070.00715.53
7.0.200.0050.00516.12
7.0.190.0070.01015.44
7.0.180.0090.00615.41
7.0.170.0120.00615.35
7.0.160.0070.00715.14
7.0.150.0000.01315.42
7.0.140.0000.01315.47
7.0.130.0060.00915.25
7.0.120.0110.00315.48
7.0.110.0030.00715.45
7.0.100.0050.04417.74
7.0.90.0050.04217.70
7.0.80.0230.03317.67
7.0.70.0100.03817.55
7.0.60.0090.04217.79
7.0.50.0090.04117.86
7.0.40.0100.04116.76
7.0.30.0130.04216.60
7.0.20.0070.03816.74
7.0.10.0130.04016.79
7.0.00.0030.04316.80
5.6.380.0030.01214.18
5.6.370.0000.00914.35
5.6.360.0070.01014.53
5.6.350.0000.00814.37
5.6.340.0100.00014.35
5.6.330.0030.01214.45
5.6.320.0060.00614.45
5.6.310.0080.00614.10
5.6.300.0030.01014.79
5.6.290.0060.01014.22
5.6.280.0090.03717.78
5.6.270.0000.01514.63
5.6.260.0080.00314.25
5.6.250.0110.04017.67
5.6.240.0150.03817.46
5.6.230.0080.03917.42
5.6.220.0100.04217.39
5.6.210.0100.03117.31
5.6.200.0090.04417.57
5.6.190.0100.04017.58
5.6.180.0130.03817.66
5.6.170.0140.03817.84
5.6.160.0070.02717.70
5.6.150.0050.04217.45
5.6.140.0030.05117.72
5.6.130.0080.03817.71
5.6.120.0150.03317.56
5.6.110.0070.03917.61
5.6.100.0100.04317.43
5.6.90.0040.04417.73
5.6.80.0120.04017.30
5.6.70.0090.02317.29
5.6.60.0080.02017.33
5.6.50.0050.04317.29
5.6.40.0020.04817.28
5.6.30.0020.04517.31
5.6.20.0070.04117.35
5.6.10.0080.04017.31
5.6.00.0030.03717.22
5.5.380.0100.02515.90
5.5.370.0090.03815.89
5.5.360.0100.04015.85
5.5.350.0100.04015.82
5.5.340.0020.04415.97
5.5.330.0070.04216.11
5.5.320.0130.03816.00
5.5.310.0030.04316.00
5.5.300.0110.04015.92
5.5.290.0050.04316.06
5.5.280.0060.04516.06
5.5.270.0060.02316.04
5.5.260.0050.03115.97
5.5.250.0070.03715.97
5.5.240.0090.04115.75
5.5.230.0050.02315.73
5.5.220.0120.02015.73
5.5.210.0030.03215.69
5.5.200.0030.03815.69
5.5.190.0070.04215.72
5.5.180.0040.04415.67
5.5.170.0000.01211.18
5.5.160.0080.04215.74
5.5.150.0070.04215.70
5.5.140.0050.02215.69
5.5.130.0050.04015.60
5.5.120.0140.03015.72
5.5.110.0030.03115.74
5.5.100.0070.03315.61
5.5.90.0130.01715.57
5.5.80.0080.02715.65
5.5.70.0080.03215.62
5.5.60.0100.03315.68
5.5.50.0060.02515.65
5.5.40.0100.04315.67
5.5.30.0100.04215.70
5.5.20.0110.02115.65
5.5.10.0080.02515.59
5.5.00.0060.04215.66

preferences:
19.13 ms | 401 KiB | 5 Q