3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface PolicyRule { public function isSatisfiedBy(string $aValue); } abstract class EnforcedRule implements PolicyRule { const UNENFORCED = 0; const ENFORCED = 1; private $enforcementType; protected function isEnforced() : bool { return ($this->enforcementType == self::ENFORCED); } protected function setEnforcementType(int $aType) { $this->guardEnforcementType($aType); $this->enforcementType = $aType; } protected function guardEnforcementType(int $aType) : void { if (! in_array($aType, [self::UNENFORCED, self::ENFORCED])) { throw new InvalidEnforcementTypeException($aType); } } } abstract class MinimumCharacterCountRule extends EnforcedRule { const DEFAULT_MINIMUM = 1; protected $regexPattern; protected $minimumCount = self::DEFAULT_MINIMUM; public function isSatisfiedBy(string $aValue) : bool { if (! $this->isEnforced()) { return true; } $matches = []; preg_match_all($this->regexPattern, $aValue, $matches); return (count($matches[0]) >= $this->minimumCount); } } class NumericRule extends MinimumCharacterCountRule { protected $regexPattern = '/[0-9]/'; public function __construct(int $aMinimumCount = self::DEFAULT_MINIMUM, int $anEnforcementType = self::ENFORCED) { $this->setEnforcementType($anEnforcementType); $this->minimumCount = $aMinimumCount; } } class SpecialCharacterRule extends MinimumCharacterCountRule { protected $regexPattern = '/[~`#\$%\^&\*\(\)\-_\+\|,\.<>]/'; public function __construct(int $aMinimumCount = self::DEFAULT_MINIMUM, int $anEnforcementType = self::ENFORCED) { $this->setEnforcementType($anEnforcementType); $this->minimumCount = $aMinimumCount; } } $numeric = new NumericRule(); var_dump($numeric->isSatisfiedBy("for5")); $special = new SpecialCharacterRule(); var_dump($special->isSatisfiedBy("for5"));
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
bool(true) bool(false)
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 bool(true) bool(false)
Output for 7.0.0 - 7.0.20
Fatal error: Uncaught TypeError: Return value of EnforcedRule::guardEnforcementType() must be an instance of void, none returned in /in/Z8nJt:31 Stack trace: #0 /in/Z8nJt(22): EnforcedRule->guardEnforcementType(1) #1 /in/Z8nJt(61): EnforcedRule->setEnforcementType(1) #2 /in/Z8nJt(77): NumericRule->__construct() #3 {main} thrown in /in/Z8nJt on line 31
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/Z8nJt on line 15
Process exited with code 255.

preferences:
153.4 ms | 402 KiB | 183 Q