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 git.master, git.master_jit, rfc.property-hooks
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"

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
52.95 ms | 402 KiB | 8 Q