3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace { try { $parent = new Package\MyParent(); var_dump($parent); $parent->test(); } catch (\Throwable $e) { var_dump($e); } try { $pubChild = new Package\MyParent\PublicChild();//create new public child (possible) } catch (\Throwable $e) { var_dump($e); } try { $protChild = new Package\MyParent\ProtectedChild(); //create new protected child (ERROR) } catch (\Throwable $e) { var_dump($e); } class Package { /* protect constructor so that objects can't be instantiated from outside * Since all classes inherit from Package class, they can instantiate eachother * simulating protected InnerClasses */ protected function __construct() { } /* This magic method is called everytime an inaccessible method is called * (either by visibility contrains or it doesn't exist) * Here we are simulating shared protected methods across "package" classes * This method is inherited by all child classes of Package */ public function __call($method, $args) { //class name $class = get_class($this); /* we check if a method exists, if not we throw an exception * similar to the default error */ if (method_exists($this, $method)) { /* The method exists so now we want to know if the * caller is a child of our Package class. If not we throw an exception * Note: This is a kind of a dirty way of finding out who's * calling the method by using debug_backtrace and reflection */ $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); if (isset($trace[2])) { $ref = new ReflectionClass($trace[2]['class']); if ($ref->isSubclassOf(__CLASS__)) { return $this->$method($args); } } throw new RuntimeException("Call to private method $class::$method()"); } else { throw new RuntimeException("Call to undefined method $class::$method()"); } } } } namespace Package { class MyParent extends \Package { public $publicChild; protected $protectedChild; public function __construct() { //instantiate public child inside parent $this->publicChild = new \Package\MyParent\PublicChild(); //instantiate protected child inside parent $this->protectedChild = new \Package\MyParent\ProtectedChild(); } public function test(): void { echo "Call from parent -> "; $this->publicChild->protectedMethod(); $this->protectedChild->protectedMethod(); echo "<br>Siblings<br>"; $this->publicChild->callSibling($this->protectedChild); } } } namespace Package\MyParent { class PublicChild extends \Package { //Makes the constructor public, hence callable from outside public function __construct() { } protected function protectedMethod(): void { echo "I'm " . get_class($this) . " protected method<br>"; } protected function callSibling($sibling): void { echo "Call from " . get_class($this) . " -> "; $sibling->protectedMethod(); } } class ProtectedChild extends \Package { protected function protectedMethod(): void { echo "I'm " . get_class($this) . " protected method<br>"; } protected function callSibling($sibling): void { echo "Call from " . get_class($this) . " -> "; $sibling->protectedMethod(); } } }

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.60.0130.01018.31
8.3.50.0120.00316.76
8.3.40.0120.00418.70
8.3.30.0090.00618.83
8.3.20.0000.00819.07
8.3.10.0090.00018.95
8.3.00.0040.00419.67
8.2.180.0090.00618.36
8.2.170.0090.00622.96
8.2.160.0090.00619.03
8.2.150.0060.00325.66
8.2.140.0060.00324.66
8.2.130.0080.00019.64
8.2.120.0040.00426.35
8.2.110.0090.00022.21
8.2.100.0060.00617.84
8.2.90.0030.00617.75
8.2.80.0000.00918.92
8.2.70.0030.00518.03
8.2.60.0060.00317.93
8.2.50.0030.00618.10
8.2.40.0050.00319.41
8.2.30.0040.00419.45
8.2.20.0050.00318.04
8.2.10.0000.00718.20
8.2.00.0050.00319.40
8.1.280.0090.00625.92
8.1.270.0090.00023.87
8.1.260.0000.00826.35
8.1.250.0000.00828.09
8.1.240.0000.01024.03
8.1.230.0170.00018.97
8.1.220.0000.00817.74
8.1.210.0040.00418.77
8.1.200.0000.00817.35
8.1.190.0000.00817.35
8.1.180.0000.00818.10
8.1.170.0060.00318.47
8.1.160.0040.00418.96
8.1.150.0070.00018.96
8.1.140.0000.00717.64
8.1.130.0070.00018.95
8.1.120.0050.00317.39
8.1.110.0030.00617.47
8.1.100.0100.00017.46
8.1.90.0040.00417.47
8.1.80.0060.00317.50
8.1.70.0090.00017.49
8.1.60.0000.00817.67
8.1.50.0080.00017.58
8.1.40.0040.00417.57
8.1.30.0030.00617.68
8.1.20.0040.00417.66
8.1.10.0050.00317.55
8.1.00.0040.00417.54
8.0.300.0000.00819.73
8.0.290.0040.00416.75
8.0.280.0000.00718.48
8.0.270.0000.00717.30
8.0.260.0050.00517.19
8.0.250.0000.00817.05
8.0.240.0040.00417.04
8.0.230.0000.00717.06
8.0.220.0020.00516.99
8.0.210.0040.00416.86
8.0.200.0000.00817.06
8.0.190.0000.00717.06
8.0.180.0080.00016.94
8.0.170.0090.00016.96
8.0.160.0040.00416.88
8.0.150.0000.00816.88
8.0.140.0040.00416.93
8.0.130.0050.00013.34
8.0.120.0040.00416.98
8.0.110.0040.00417.02
8.0.100.0000.00816.93
8.0.90.0040.00416.93
8.0.80.0040.01716.92
8.0.70.0070.00016.88
8.0.60.0000.00716.82
8.0.50.0040.00416.89
8.0.30.0070.01417.17
8.0.20.0080.01317.40
8.0.10.0050.00217.11
8.0.00.0060.01216.58
7.4.330.0060.00015.55
7.4.320.0060.00016.55
7.4.300.0040.00416.46
7.4.290.0030.00316.43
7.4.280.0040.00416.61
7.4.270.0070.00016.64
7.4.260.0000.00816.47
7.4.250.0080.00016.59
7.4.240.0040.00416.63
7.4.230.0000.00716.46
7.4.220.0040.00416.42
7.4.210.0070.00816.52
7.4.200.0000.00716.63
7.4.160.0080.00916.60
7.4.140.0090.01117.86
7.4.130.0190.00616.52
7.4.120.0130.00616.55
7.4.110.0100.00716.61
7.4.100.0120.00916.46
7.4.90.0190.00516.55
7.4.80.0130.00619.39
7.4.70.0060.01216.62
7.4.60.0080.01116.56
7.4.50.0090.00916.59
7.4.40.0060.01316.43
7.4.00.0070.00915.11
7.3.330.0000.00513.57
7.3.320.0060.00013.32
7.3.310.0050.00316.45
7.3.300.0030.00516.57
7.3.290.0030.00316.34
7.3.280.0060.01516.43
7.3.260.0100.00916.61
7.3.240.0110.00816.54
7.3.230.0120.01216.49
7.3.210.0150.00816.39
7.3.200.0100.00716.61
7.3.190.0070.01316.48
7.3.180.0130.00316.57
7.3.170.0080.00816.55
7.3.160.0140.00316.61
7.3.120.0100.00715.13
7.3.110.0100.00715.05
7.3.100.0070.01014.81
7.3.90.0000.01214.59
7.3.80.0040.01114.60
7.3.70.0060.00614.96
7.3.60.0070.01115.05
7.3.50.0130.00014.97
7.3.40.0060.00614.83
7.3.30.0030.01315.00
7.3.20.0080.00416.56
7.3.10.0060.00616.45
7.3.00.0060.01016.68
7.2.330.0160.00316.86
7.2.320.0070.01316.42
7.2.310.0100.01016.50
7.2.300.0170.00416.45
7.2.290.0110.00816.93
7.2.250.0140.00714.93
7.2.240.0030.01714.96
7.2.230.0090.00315.07
7.2.220.0070.00714.91
7.2.210.0090.00915.38
7.2.200.0110.00415.23
7.2.190.0050.00515.23
7.2.180.0110.00714.95
7.2.170.0040.01115.10
7.2.130.0060.01216.39
7.2.120.0000.01716.24
7.2.110.0170.00916.56
7.2.100.0120.00416.66
7.2.90.0120.00316.37
7.2.80.0090.00916.61
7.2.70.0030.01316.51
7.2.60.1950.00515.93
7.2.50.1950.00415.57
7.2.40.1570.01016.09
7.2.30.1460.01116.11
7.2.20.1080.00415.82
7.2.10.1570.00716.10
7.2.00.1520.01116.03
7.1.330.0030.01015.71
7.1.320.0040.01115.92
7.1.310.0060.00315.92
7.1.300.0090.00616.04
7.1.290.0080.00815.90
7.1.280.0060.00615.77
7.1.270.0090.00616.01
7.1.260.0080.00815.97
7.1.250.0080.01215.72
7.1.200.0060.01015.43
7.1.170.3110.00613.77
7.1.160.2630.00816.51
7.1.150.3000.01016.82
7.1.140.2840.01216.75
7.1.130.2810.01517.04
7.1.120.2610.00916.99
7.1.110.3070.01316.28
7.1.100.3850.00516.03
7.1.90.4540.01416.15
7.1.80.3890.01015.74
7.1.70.4410.01615.47
7.1.60.4950.01933.15
7.1.50.4930.01132.92
7.1.40.5180.01332.84
7.1.30.5010.01432.79
7.1.20.5260.01332.69
7.1.10.5390.00814.89
7.1.00.3940.01114.96

preferences:
62.26 ms | 401 KiB | 5 Q