3v4l.org

run code in 300+ PHP versions simultaneously
<?php enum ProductCategories: string { case TSHIRT = 't_shirt'; case SHOES = 'shoes'; } abstract class Base { protected static array $categoryMappings; public static function getLocalCategoryId(ProductCategories $category): ?string { // Use $category->value to access the scalar value of the enum return static::$categoryMappings[$category->value] ?? null; } public static function getCategoryFromLocalId(string $categoryId): ?ProductCategories { // Flip the array to find the original enum based on categoryId $inverted = array_flip(static::$categoryMappings); if (array_key_exists($categoryId, $inverted)) { // Use ProductCategories::from to create enum from the string value return ProductCategories::from($inverted[$categoryId]); } return null; } abstract public function getByCategory(ProductCategories $category); } class A extends Base { protected static array $categoryMappings = [ ProductCategories::TSHIRT->value => '1', ProductCategories::SHOES->value => '2', ]; public function getByCategory(ProductCategories $category) { $localCategory = static::getLocalCategoryId($category); // Makes API call and returns results return $localCategory; } } class B extends Base { protected static array $categoryMappings = [ ProductCategories::TSHIRT->value => 't-shirt', ProductCategories::SHOES->value => 'mens-shoes', ]; public function getByCategory(ProductCategories $category) { $localCategory = static::getLocalCategoryId($category); // Makes API call and returns results return $localCategory; } } // Test cases echo A::getLocalCategoryId(ProductCategories::TSHIRT); // 1 echo "\n"; echo B::getLocalCategoryId(ProductCategories::TSHIRT); // t-shirt echo "\n"; echo A::getCategoryFromLocalId('1') === B::getCategoryFromLocalId('t-shirt') ? 'Same' : 'Not same'; echo "\n"; echo A::getLocalCategoryId(ProductCategories::SHOES); // 2 echo "\n"; echo B::getLocalCategoryId(ProductCategories::SHOES); // mens-shoes echo "\n"; echo "\n\n\n"; $aClass = new A(); echo $aClass->getByCategory(ProductCategories::TSHIRT); echo "\n"; $bClass = new B(); echo $bClass->getByCategory(ProductCategories::TSHIRT);

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.120.0360.01016.53
8.3.110.0350.01116.65
8.3.100.0340.01316.50
8.3.90.0370.00716.43
8.3.80.0350.00716.18
8.3.70.0330.01116.22
8.3.60.0410.00616.58
8.3.50.0230.01216.57
8.3.40.0160.00717.41
8.3.30.0150.01117.40
8.3.20.0320.00617.12
8.3.10.0280.00617.63
8.3.00.0120.00917.24
8.2.240.0200.01216.84
8.2.230.0290.00616.08
8.2.220.0190.01616.52
8.2.210.0310.00816.24
8.2.200.0330.00716.36
8.2.190.0320.00916.69
8.2.180.0310.00316.49
8.2.170.0300.01117.51
8.2.160.0310.01417.56
8.2.150.0400.01017.47
8.2.140.0360.00617.36
8.2.130.0350.00617.38
8.2.120.0300.01117.21
8.2.110.0290.01317.53
8.2.100.0290.01317.37
8.2.90.0370.00417.62
8.2.80.0370.01317.42
8.2.70.0370.00717.36
8.2.60.0370.00417.29
8.2.50.0380.00317.58
8.2.40.0330.01317.38
8.2.30.0270.00917.45
8.2.20.0370.00017.48
8.2.10.0240.01717.37
8.2.00.0220.01117.37
8.1.300.0200.01615.96
8.1.290.0320.00415.84
8.1.280.0310.01116.00
8.1.270.0390.00717.08
8.1.260.0370.00017.08
8.1.250.0210.00716.94
8.1.240.0130.00517.12
8.1.230.0130.00517.25
8.1.220.0270.00417.11
8.1.210.0250.01116.96
8.1.200.0190.01517.18
8.1.190.0360.00416.85
8.1.180.0310.00917.15
8.1.170.0190.01916.99
8.1.160.0290.01116.99
8.1.150.0250.00817.10
8.1.140.0300.00417.19
8.1.130.0170.01316.86
8.1.120.0200.01317.15
8.1.110.0320.00717.21
8.1.100.0400.00717.00
8.1.90.0320.00817.01
8.1.80.0240.01217.05
8.1.70.0270.01016.98
8.1.60.0280.00717.12
8.1.50.0360.00417.45
8.1.40.0260.01017.27
8.1.30.0330.00017.37
8.1.20.0230.01617.29
8.1.10.0320.00417.33
8.1.00.0220.00417.14

preferences:
27.9 ms | 403 KiB | 5 Q