3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Collection extends ArrayObject { /** * @var string */ protected $expectedValueType = ''; /** * Collection constructor. * * @param string $expectedValueType * @param array $initialValues */ public function __construct(string $expectedValueType, array $initialValues = []) { $this->setExpectedValueType($expectedValueType); foreach ($initialValues as $initialValue) $this->append($initialValue); } /** * @param $value */ public function append($value) { if (!$this->validateType($value)) throw new \InvalidArgumentException('Expected value type for this collection is ' . $this->getExpectedValueType() . ', ' . gettype($value) . ' given.'); parent::append($value); } /** * @param mixed $value * * @return bool */ public function contains($value): bool { foreach ($this as $v) { if ($v == $value) return true; } return false; } /** * @param $value * * @return false|int|string|mixed */ public function getKey($value) { foreach ($this as $key => $v) { if ($v == $value) return $key; } return false; } /** * @inheritdoc */ public function offsetSet($offset, $value) { if (!$this->validateType($value)) throw new \InvalidArgumentException('Expected value type for this collection is ' . $this->getExpectedValueType() . ', ' . gettype($value) . ' given.'); parent::offsetSet($offset, $value); } /** * @param mixed $value */ public function remove($value) { if (!$this->contains($value)) throw new \InvalidArgumentException('The given value does not exist in this collection.'); $this->offsetUnset($this->getKey($value)); } /** * @param $value * * @return bool */ public function validateType($value): bool { $expectedValueType = $this->getExpectedValueType(); switch ($expectedValueType) { case 'string': return is_string($value); case 'int': case 'integer': return is_int($value); case 'float': case 'double': return is_float($value); case 'array': return is_array($value); case 'object': return is_object($value); case 'callable': return is_callable($value); default: return ($value instanceof $expectedValueType); } } /** * @return string */ public function getExpectedValueType(): string { return $this->expectedValueType; } /** * @param string $expectedValueType */ public function setExpectedValueType(string $expectedValueType) { if (in_array(strtolower($expectedValueType), ['string', 'int', 'integer', 'float', 'double', 'bool', 'boolean', 'array', 'object', 'callable'])) $expectedValueType = strtolower($expectedValueType); $this->expectedValueType = $expectedValueType; } } $coll = new Collection ('string',['a','b']); foreach($coll as $item){ var_dump($item); }
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Return type of Collection::offsetSet($offset, $value) should either be compatible with ArrayObject::offsetSet(mixed $key, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/leppJ on line 61 Deprecated: Return type of Collection::append($value) should either be compatible with ArrayObject::append(mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/leppJ on line 24 string(1) "a" string(1) "b"
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 Deprecated: Return type of Collection::offsetSet($offset, $value) should either be compatible with ArrayObject::offsetSet(mixed $key, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/leppJ on line 61 Deprecated: Return type of Collection::append($value) should either be compatible with ArrayObject::append(mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/leppJ on line 24 string(1) "a" string(1) "b"
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
string(1) "a" string(1) "b"

preferences:
174.22 ms | 402 KiB | 187 Q