3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class MagicMethod { public const NAME_AFTER_DESERIALIZATION = 'new'; public const INVOKE_MESSAGE = 'I am not a function, I am a class!'; public const DESTRUCT_MESSAGE = 'I\'m going to heaven.'; public const SET_STATE_MESSAGE = 'Is it working?'; public const VAR_DUMP_MESSAGE = 'I\'m a dump'; /** * @var int */ private static $cloneCounter = 1; /** * @var null | int */ private $cloneNumber; /** * @var bool */ private $isClone; /** * @var string */ private $name; /** * @var int */ private $number; /** * MagicMethod constructor. * @link https://kursphp.com/programowanie-obiektowe-php/konstruktor/ Multiple constructor & copy constructor. * @param string $name * @param int $number */ public function __construct(string $name, int $number) { $this->cloneNumber = null; $this->isClone = false; $this->name = $name; $this->number = $number; } public function isClone(): bool { return $this->isClone; } public function getCloneNumber(): ?int { return $this->cloneNumber; } /** * Method used to test magic method '__get' */ public function getNumber(): int { return $this->number; } /** * Method used to test magic methods '__sleep' & '__wakeup' */ public function getName(): string { return $this->name; } /** * @param string $name * @param mixed[] $arguments * @throws \Exception */ public function __call(string $name, array $arguments) { throw new \Exception('Method does not exist.'); } /** * @param string $name * @param mixed[] $arguments * @throws \Exception */ public static function __callStatic(string $name, array $arguments) { throw new \Exception('Static method does not exist.'); } /** * @link https://sekurak.pl/php-object-injection-malo-znana-krytyczna-klasa-podatnosci/ How to avoid problems. * @return string */ public function __toString(): string { return $this->name; } /** * @link https://www.owasp.org/index.php/PHP_Object_Injection How to avoid problems. * No returned value. */ public function __destruct() { } /** * Warning: magic is slower (than getters/setters) * @param string $name * @return mixed * @throws \Exception */ public function __get(string $name) { $methodName = "get{$name}"; if (method_exists($this, $methodName)) { return $this->$methodName(); } throw new \Exception('No access to property or property does not exist.'); // (note) worse way: trigger_error('message', E_USER_NOTICE); } /** * @param string $name * @param mixed $value * @throws \Exception */ public function __set(string $name, $value): void { throw new \Exception('No access to property or property does not exist.'); // (note) or logic: check name, check and filter value, fun with reflection, etc. } /** * @link https://www.php.net/manual/en/language.oop5.magic.php#object.sleep docs. */ public function __sleep(): array { return ['number']; } /** * @link https://www.owasp.org/index.php/PHP_Object_Injection How to avoid problems. */ public function __wakeup(): void { $this->name = self::NAME_AFTER_DESERIALIZATION; } /** * Method is called when a script tries to call an object as a function. */ public function __invoke(): string { return self::INVOKE_MESSAGE; } /** * When the property does not exist or is protected/private, return false. * @param $name * @return bool */ public function __isset($name): bool { return false; } /** * When the property does not exist or is protected/private, throw Exception. * The exception is thrown only for testing purposes. * @param $name * @throws \Exception */ public function __unset($name): void { throw new \Exception($name . 'property does not exist or is protected/private.'); } /** * Method is run for the clone, not the original object. * @link https://www.youtube.com/watch?v=Sz-Abh8opLo just a movie trailer. */ public function __clone() { $this->isClone = true; $this->cloneNumber = static::$cloneCounter; static::$cloneCounter++; } /** * @link https://stackoverflow.com/questions/46441958/what-is-the-real-purpose-of-magic-method-set-state-in-php Better * than official docs. * @link http://docs.php.net/manual/pl/function.var-export.php var_export() docs * @param array $array * @return string */ public static function __set_state(array $array): string { return self::SET_STATE_MESSAGE; } public function __debugInfo(): array { return [ 'anything' => 'anything' ]; } } $object = new MagicMethod('name', 44); var_dump($object);

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.0110.01116.75
8.3.60.0100.00718.20
8.3.50.0070.00718.09
8.3.40.0100.01418.67
8.3.30.0080.00618.45
8.3.20.0080.00022.04
8.3.10.0040.00423.49
8.3.00.0030.00622.37
8.2.180.0090.00618.54
8.2.170.0110.00422.96
8.2.160.0100.00720.39
8.2.150.0030.00624.18
8.2.140.0040.00424.66
8.2.130.0070.00026.16
8.2.120.0040.00426.16
8.2.110.0090.00021.12
8.2.100.0110.00017.78
8.2.90.0000.00720.47
8.2.80.0000.00817.97
8.2.70.0040.00417.80
8.2.60.0120.00017.90
8.2.50.0050.00418.10
8.2.40.0000.00920.62
8.2.30.0070.00017.96
8.2.20.0000.00817.96
8.2.10.0040.00419.14
8.2.00.0000.00719.04
8.1.280.0150.00625.92
8.1.270.0050.00523.84
8.1.260.0070.00026.35
8.1.250.0000.00728.09
8.1.240.0030.00719.07
8.1.230.0090.00320.91
8.1.220.0000.00917.74
8.1.210.0040.00418.90
8.1.200.0070.00317.22
8.1.190.0050.00317.11
8.1.180.0040.00418.10
8.1.170.0040.00418.58
8.1.160.0000.00718.72
8.1.150.0000.00718.71
8.1.140.0000.00720.86
8.1.130.0070.00019.04
8.1.120.0000.00717.35
8.1.110.0000.00717.42
8.1.100.0000.00717.38
8.1.90.0030.00617.40
8.1.80.0000.00817.27
8.1.70.0000.00917.29
8.1.60.0040.00417.55
8.1.50.0000.00817.32
8.1.40.0090.00017.42
8.1.30.0000.00817.50
8.1.20.0030.00617.57
8.1.10.0030.00617.42
8.1.00.0050.00317.25
8.0.300.0000.00819.72
8.0.290.0000.00716.75
8.0.280.0000.00718.35
8.0.270.0040.00417.11
8.0.260.0060.00016.80
8.0.250.0000.00716.91
8.0.240.0040.00416.98
8.0.230.0000.00816.95
8.0.220.0030.00516.84
8.0.210.0000.00716.75
8.0.200.0000.00816.89
8.0.190.0000.00716.91
8.0.180.0040.00416.78
8.0.170.0040.00416.88
8.0.160.0000.00716.84
8.0.150.0040.00416.80
8.0.140.0070.00016.89
8.0.130.0030.00313.32
8.0.120.0000.00716.74
8.0.110.0040.00416.84
8.0.100.0000.00716.81
8.0.90.0060.00316.69
8.0.80.0070.00716.84
8.0.70.0080.00016.93
8.0.60.0000.00716.77
8.0.50.0000.00716.74
8.0.30.0080.01016.97
8.0.20.0110.01117.19
8.0.10.0070.00016.94
8.0.00.0080.00816.68
7.4.330.0050.00016.67
7.4.320.0080.00016.39
7.4.300.0060.00016.51
7.4.290.0000.00716.46
7.4.280.0040.00416.68
7.4.270.0070.00416.50
7.4.260.0000.00513.39
7.4.250.0000.00816.63
7.4.240.0010.00616.56
7.4.230.0050.00216.64
7.4.220.0050.00216.51
7.4.210.0070.00716.62
7.4.200.0000.00716.75
7.4.130.0150.00416.64
7.4.120.0070.01916.41
7.4.110.0090.00916.38
7.4.100.0100.00716.58
7.4.90.0090.00816.34
7.4.80.0160.00619.39
7.4.70.0100.01316.55
7.4.60.0090.01216.43
7.4.50.0060.01016.17
7.4.40.0150.00316.46
7.4.00.0090.00614.96
7.3.330.0000.00816.36
7.3.320.0000.00613.18
7.3.310.0030.00316.24
7.3.300.0000.00716.36
7.3.290.0110.00616.38
7.3.280.0090.01416.34
7.3.260.0100.01016.43
7.3.240.0080.00916.42
7.3.230.0120.00616.17
7.3.210.0110.00716.32
7.3.200.0030.01516.25
7.3.190.0060.01216.46
7.3.180.0060.01216.27
7.3.170.0160.00616.45
7.3.160.0120.00616.55
7.3.90.0090.00614.71
7.3.80.0060.01215.11
7.3.70.0090.00914.87
7.3.60.0030.01014.84
7.3.50.0060.01314.68
7.3.40.0110.00414.88
7.3.30.0100.00314.96
7.3.20.0030.01016.25
7.3.10.0070.00716.41
7.3.00.0120.00615.46
7.2.330.0160.00516.70
7.2.320.0070.01316.68
7.2.310.0130.00316.38
7.2.300.0120.00916.58
7.2.290.0120.00616.46
7.2.220.0060.00914.90
7.2.210.0120.00614.95
7.2.200.0070.00615.03
7.2.190.0140.00014.98
7.2.180.0030.01714.87
7.2.170.0030.01015.09
7.2.160.0100.00315.03
7.2.150.0080.00616.77
7.2.140.0070.00716.89
7.2.130.0040.01116.91
7.2.120.0000.01516.64
7.2.110.0120.00316.73
7.2.100.0120.00316.97
7.2.90.0070.00716.84
7.2.80.0090.00616.97
7.2.70.0140.00816.86
7.2.60.0060.00916.59
7.2.50.0000.01416.83
7.2.40.0060.00916.95
7.2.30.0070.00716.83
7.2.20.0030.01216.75
7.2.10.0030.01317.00
7.2.00.0060.00916.54
7.1.320.0040.00915.65
7.1.310.0090.00915.79
7.1.300.0060.00715.59
7.1.290.0060.01115.70
7.1.280.0100.00315.72
7.1.270.0070.00715.72
7.1.260.0030.01315.74
7.1.250.0000.01315.75

preferences:
50.09 ms | 401 KiB | 5 Q