3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Collection { private $items; private static $macros = []; public static function macro($name, callable $macro) { static::$macros[$name] = Closure::fromCallable($macro); } public static function mixin($class) { foreach (get_class_methods($class) as $method) { static::macro($method, $class->$method()); } } public function __construct(array $items) { $this->items = $items; } public function map(callable $callback) { return new static(array_map($callback, $this->items)); } public function all() { return $this->items; } public function exampleClosure() { return function () { return $this->items; }; } public function __call($method, $args) { return static::$macros[$method]->bindTo($this, static::class)(...$args); } public static function __callStatic($method, $args) { return static::$macros[$method]->bindTo(null, static::class)(...$args); } } Collection::macro("flatMap", function (callable $callback) { $tmp = []; foreach ($this->items as $item) { $tmp = array_merge($tmp, $callback($item)); } return new static($tmp); }); $items = new Collection(["a", "b", "c"]); $items = $items->flatMap(function ($item) { return [$item, $item]; }); if ($items->all() === ["a", "a", "b", "b", "c", "c"]) { echo "it works"; } // This is the thing that is deprecated and causes a warning: // Here we're removing $this from a closure that isn't static // All non-static closures will have a $this in PHP 8.x when defined in a method try { var_dump( $items->exampleClosure()->bindTo(null)() ); } catch (\Throwable $e) { // We'll get an error about using $this is a non-object context // This will no longer be possible after PHP 7.4 var_dump($e); } // You can definitely still do this: class UnrelatedClass { private $items = ["foo", "bar"]; } var_dump( $items->exampleClosure()->bindTo(new UnrelatedClass, UnrelatedClass::class)() ); // The only thing that could cause problems is: // methods meant to be called statically in a mixin that were not defined with static: class SomeMixin { public function range() { return function ($start, $end) { return new static(range($start, $end)); }; } public function staticRange() { return static function ($start, $end) { return new static(range($start, $end)); }; } } Collection::mixin(new SomeMixin()); // This will error because: // 1. the above is not defined with static // 2. The closure is written inside a class var_dump(Collection::range(0, 10)); // The following will not because the closure was defined with static var_dump(Collection::staticRange(0, 10)); // Basically the change prevents the creation of methods and closures that can be used as instance and static methods at the same time by dynamically checking $this inside the function.

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.5.10.0090.01116.86
8.5.00.0110.01020.15
8.4.150.0020.00014.05
8.4.140.0090.01317.59
8.4.130.0130.00819.50
8.4.120.0100.01023.99
8.4.110.0120.00822.29
8.4.100.0100.01118.88
8.4.90.0070.01018.90
8.4.80.0140.00619.02
8.4.70.0120.00720.63
8.4.60.0140.00718.80
8.4.50.0130.00818.73
8.4.40.0080.01219.49
8.4.30.0100.00618.64
8.4.20.0130.00318.78
8.4.10.0140.00317.87
8.3.280.0130.00818.60
8.3.270.0120.00816.84
8.3.260.0110.00916.70
8.3.250.0150.00519.16
8.3.240.0120.00916.98
8.3.230.0110.00816.84
8.3.220.0120.00419.16
8.3.210.0100.00818.56
8.3.200.0040.00616.69
8.3.190.0110.01017.18
8.3.180.0120.00817.09
8.3.170.0070.01120.70
8.3.160.0000.00917.11
8.3.150.0030.00517.20
8.3.140.0000.00818.53
8.3.130.0070.01318.63
8.3.120.0030.00620.43
8.3.110.0040.00420.94
8.3.100.0030.00624.06
8.3.90.0000.01026.77
8.3.80.0090.00018.55
8.3.70.0100.00616.88
8.3.60.0100.00618.58
8.3.50.0100.00921.20
8.3.40.0040.01819.07
8.3.30.0090.00618.76
8.3.20.0080.00020.85
8.3.10.0040.00421.86
8.3.00.0030.00523.56
8.2.290.0060.00220.54
8.2.280.0050.00418.42
8.2.270.0080.00017.08
8.2.260.0040.01117.00
8.2.250.0100.00316.55
8.2.240.0100.01019.23
8.2.230.0110.01122.58
8.2.220.0000.00937.54
8.2.210.0030.00926.77
8.2.200.0030.00618.88
8.2.190.0150.00416.58
8.2.180.0110.00716.75
8.2.170.0160.00322.96
8.2.160.0070.00720.32
8.2.150.0060.00324.18
8.2.140.0080.00024.66
8.2.130.0040.00426.16
8.2.120.0060.00326.16
8.2.110.0090.00020.39
8.2.100.0080.00817.84
8.2.90.0040.00418.02
8.2.80.0080.00019.31
8.2.70.0060.00317.93
8.2.60.0000.00818.16
8.2.50.0090.00018.10
8.2.40.0060.00319.34
8.2.30.0080.00019.37
8.2.20.0030.00618.05
8.2.10.0050.00219.23
8.2.00.0060.00319.36
8.1.330.0140.00522.25
8.1.320.0130.00816.01
8.1.310.0030.00516.95
8.1.300.0050.00520.10
8.1.290.0000.00930.84
8.1.280.0070.01425.92
8.1.270.0000.00821.00
8.1.260.0040.00426.35
8.1.250.0000.00828.09
8.1.240.0060.00323.83
8.1.230.0090.00319.19
8.1.220.0030.00617.74
8.1.210.0050.00318.77
8.1.200.0000.00917.60
8.1.190.0000.00817.35
8.1.180.0040.00718.10
8.1.170.0030.00518.63
8.1.160.0090.00318.86
8.1.150.0090.00018.86
8.1.140.0030.00620.82
8.1.130.0070.00020.19
8.1.120.0040.00417.59
8.1.110.0080.00017.43
8.1.100.0000.00817.48
8.1.90.0000.00817.55
8.1.80.0080.00017.46
8.1.70.0030.00317.55
8.1.60.0040.00417.64
8.1.50.0060.00317.59
8.1.40.0030.00517.53
8.1.30.0050.00317.72
8.1.20.0030.00517.72
8.1.10.0070.00317.67
8.1.00.0000.00817.46
8.0.300.0050.00319.80
8.0.290.0030.00616.88
8.0.280.0030.00518.55
8.0.270.0000.00717.19
8.0.260.0000.00720.13
8.0.250.0040.00416.95
8.0.240.0030.00317.10
8.0.230.0000.00717.09
8.0.220.0020.00516.95
8.0.210.0000.00717.05
8.0.200.0040.00417.09
8.0.190.0040.00417.02
8.0.180.0000.00817.02
8.0.170.0000.00916.94
8.0.160.0000.00916.95
8.0.150.0040.00416.90
8.0.140.0040.00416.86
8.0.130.0000.00713.42
8.0.120.0040.00417.00
8.0.110.0030.00516.98
8.0.100.0040.00416.96
8.0.90.0000.00916.96
8.0.80.0080.00817.04
8.0.70.0040.00416.88
8.0.60.0000.00816.85
8.0.50.0030.00517.04
8.0.30.0090.00917.21
8.0.20.0090.01117.21
8.0.10.0000.00817.08
8.0.00.0140.00816.81
7.4.330.0060.00016.89
7.4.320.0060.00016.54
7.4.300.0030.00316.62
7.4.290.0080.00016.66
7.4.280.0080.00316.61
7.4.270.0040.00416.59
7.4.260.0030.00313.28
7.4.250.0000.00716.61
7.4.240.0000.00716.56
7.4.230.0040.00416.36
7.4.220.0020.00516.67
7.4.210.0070.00816.64
7.4.200.0000.00716.46
7.4.140.0180.00417.86
7.4.130.0100.01016.68
7.4.120.0090.00916.61
7.4.110.0210.00416.66
7.4.100.0040.01516.50
7.4.90.0100.00716.71
7.4.80.0130.00319.39
7.4.70.0050.01216.70
7.4.60.0100.00616.46
7.4.50.0080.00816.34
7.4.40.0110.00816.63
7.4.10.0120.00614.84
7.4.00.0080.00915.19
7.3.330.0030.00516.36
7.3.320.0050.00013.47
7.3.310.0070.00016.37
7.3.300.0000.00716.48
7.3.290.0030.00516.47
7.3.280.0040.01416.41
7.3.260.0110.00916.50
7.3.240.0090.01316.54
7.3.230.0120.00716.59
7.3.210.0100.01016.46
7.3.200.0100.01016.73
7.3.190.0140.01016.69
7.3.180.0060.00916.40
7.3.170.0210.00316.67
7.3.160.0030.01216.53
7.3.130.0070.01114.91
7.3.120.0100.01015.03
7.3.110.0030.01714.75
7.3.100.0040.01514.84
7.3.90.0100.01014.72
7.3.80.0060.00814.86
7.3.70.0080.00814.82
7.3.60.0070.00814.84
7.3.50.0120.00514.68
7.3.40.0070.01014.76
7.3.30.0080.00814.67
7.3.20.0250.00815.26
7.3.10.0220.01115.22
7.3.00.0250.00715.27
7.2.330.0060.01316.58
7.2.320.0060.01516.84
7.2.310.0110.01516.53
7.2.300.0110.00716.61
7.2.290.0000.01916.64
7.2.260.0000.02015.39
7.2.250.0030.01015.35
7.2.240.0070.00715.28
7.2.230.0040.01215.02
7.2.220.0100.01015.21
7.2.210.0080.00815.09
7.2.200.0100.00714.96
7.2.190.0120.00615.03
7.2.180.0090.00715.07
7.2.170.0100.00714.95
7.2.160.0080.00915.01
7.2.150.0240.01115.43
7.2.140.0240.00915.20
7.2.130.0200.00915.46
7.2.120.0210.00815.37
7.2.110.0220.00615.04
7.2.100.0190.00915.32
7.2.90.0210.00915.35
7.2.80.0230.00815.37
7.2.70.0240.01115.26
7.2.60.0260.00815.37
7.2.50.0230.01115.59
7.2.40.0280.00815.61
7.2.30.0230.00715.58
7.2.20.0340.00815.98
7.2.10.0190.00615.92
7.2.00.0260.00515.97
7.1.330.0060.00915.67
7.1.320.0100.00615.74
7.1.310.0190.01114.44
7.1.300.0270.00814.35
7.1.290.0290.00814.23
7.1.280.0250.00914.35
7.1.270.0270.00714.42
7.1.260.0220.00814.22
7.1.250.0220.00914.32
7.1.240.0090.00615.63
7.1.230.0030.00615.50
7.1.220.0080.00815.81
7.1.210.0000.01215.99
7.1.200.0000.00915.97
7.1.190.0110.00415.71
7.1.180.0000.01015.83
7.1.170.0040.00415.63
7.1.160.0030.00615.74
7.1.150.0070.00715.80
7.1.140.0000.01215.82
7.1.130.0060.00615.97
7.1.120.0060.01015.77
7.1.110.0060.00315.97
7.1.100.0030.01015.75
7.1.90.0030.00615.51
7.1.80.0050.00515.78
7.1.70.0000.01215.94
7.1.60.0000.01315.67
7.1.50.0030.00915.68
7.1.40.0070.00415.71
7.1.30.0080.00415.87
7.1.20.0000.01015.82
7.1.10.0100.00015.63
7.1.00.0100.00315.69

preferences:
39.6 ms | 403 KiB | 5 Q