3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); /** * @return SplFixedArray<int> */ function getTraversable(): SplFixedArray { return SplFixedArray::fromArray([1, 2, 3]); } /** * @template A * @template B * * @param iterable<mixed, A> $lhs * @param iterable<mixed, B> $rhs * @return SplFixedArray<A|B> */ function mergeIterable(iterable $lhs, iterable $rhs): SplFixedArray { return SplFixedArray::fromArray([...$lhs, ...$rhs]); } // Expected SplFixedArray<int>, actual SplFixedArray<mixed> $_merged_iterable = mergeIterable(getTraversable(), getTraversable()); /** @psalm-trace $_merged_iterable */; /** * @template TKey * @template TValue * @implements Iterator<TKey, TValue> */ class Map implements Iterator { private int $index = 0; /** * @param list<TKey> $keys * @param list<TValue> $values */ public function __construct( private array $keys, private array $values, ) { if (count($keys) !== count($values)) { throw new \InvalidArgumentException("keys and values must be same length"); } } public function current(): mixed { return $this->values[$this->index]; } public function key(): mixed { return $this->keys[$this->index]; } public function next(): void { ++$this->index; } public function rewind(): void { $this->index = 0; } public function valid(): bool { return isset($this->values[$this->index]); } } $map1 = new Map([1.1, 1.2, 1.3], [4, 5, 6]); $map2 = new Map([new stdClass()], [true]); $_merged = mergeIterable($map1, $map2);
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Fatal error: Uncaught Error: Keys must be of type int|string during array unpacking in /in/QcTl9:23 Stack trace: #0 /in/QcTl9(81): mergeIterable(Object(Map), Object(Map)) #1 {main} thrown in /in/QcTl9 on line 23
Process exited with code 255.
Output for 8.0.1 - 8.0.30
Fatal error: Uncaught Error: Cannot unpack Traversable with non-integer keys in /in/QcTl9:23 Stack trace: #0 /in/QcTl9(81): mergeIterable(Object(Map), Object(Map)) #1 {main} thrown in /in/QcTl9 on line 23
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Parse error: syntax error, unexpected 'private' (T_PRIVATE), expecting variable (T_VARIABLE) in /in/QcTl9 on line 45
Process exited with code 255.

preferences:
165.88 ms | 401 KiB | 123 Q